【问题标题】:Get font in use when multiple font families specified in WPF在 WPF 中指定多个字体系列时获取正在使用的字体
【发布时间】:2022-02-01 20:42:52
【问题描述】:

在 WPF 中,我为我的控件设置了一个样式,因此它指定了多个字体系列:

<Style TargetType="{x:Type MyControl}">
    <Setter Property="FontFamily"
            Value="Helvetica, Arial, Segoe UI" />
</Style>

如何确定实际选择了哪种字体并用于控件?

【问题讨论】:

    标签: c# wpf xaml fonts


    【解决方案1】:

    根据this blog post

    XAML 文本呈现器 [...] 逐个字符地使用指定的字体列表(系列)。

    所以没有你问的“控件使用的字体”。

    要找出每个字符使用的字体,this response 看起来是一个不错的起点。

    【讨论】:

    • 你可以找到我这个问题相关的测试代码here
    【解决方案2】:

    我最终只需要解析FontFamilySource 属性:

    public static FontFamily GetFirstValid(this FontFamily fontFamily)
    {
        return fontFamily?.Source.Split(',')
                         .Select(p => new FontFamily(p))
                         .FirstOrDefault(f => f.GetTypefaces()
                                               .Any(t => t.TryGetGlyphTypeface(out GlyphTypeface glyph)
                                                         && glyph?.CharacterToGlyphMap.Count > 0));
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-02
      • 2016-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      相关资源
      最近更新 更多