【问题标题】:How to get list of font styles (wpf)如何获取字体样式列表(wpf)
【发布时间】:2017-08-16 14:40:13
【问题描述】:

fontdialog 从哪里得到它的字体样式列表?

FontStyles 仅包含 Normal、Oblique 和 Italic。 然而,通过各种字体的字体样式,有以下组合:medium、light、demi、bold、roman、narrow、heavy、cond……等等。

Arial 显示的字体对话框:

窄斜体, 斜体, 常规的, 窄粗体, 窄粗斜体, 胆大, 加粗斜体, 黑色的, 黑色斜交

他们从哪里得到这个列表?其中一些名字似乎没有出现在任何地方。 是否有一些具有其他指定名称或其他名称的 FamilyTypeface 重量、样式和拉伸组合列表?

感谢您的帮助!

【问题讨论】:

    标签: wpf font-family


    【解决方案1】:

    对于系统字体,您可以获得typefaces 的列表以获取FontFamily。字体本身具有所有属性的详细信息,例如粗细、样式、拉伸等。

    示例代码

    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
    
        <ListBox x:Name="fontSelector" 
                 ItemsSource="{x:Static Fonts.SystemFontFamilies}" />
    
        <ListBox x:Name="typefaceSelector" 
                 ItemsSource="{Binding SelectedItem.FamilyTypefaces, ElementName=fontSelector}" DisplayMemberPath="AdjustedFaceNames[en-US]" 
                 Grid.Column="1" />
    
        <TextBlock FontFamily="{Binding SelectedItem.Source, ElementName=fontSelector}"
                   FontStretch="{Binding SelectedItem.Stretch, ElementName=typefaceSelector}"
                   FontStyle="{Binding SelectedItem.Style, ElementName=typefaceSelector}"
                   FontWeight="{Binding SelectedItem.Weight, ElementName=typefaceSelector}"
                   Grid.ColumnSpan="2" Grid.Row="1"
                   Text="Sample" 
                   FontSize="30"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center" />
    </Grid>
    

    要在 WPF 中进一步扩展此列表,您可以使用 win-forms 中的InstalledFontCollection 获取已安装字体的列表,并将其转换为 WPF 字体类型。

    var installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
    
    // Get the array of FontFamily objects.
    var fontFamilies = installedFontCollection.Families;
    foreach(var fontFamily in fontFamilies)
    {
        var mfont = new FontFamily(fontFamily.Name);
        fontSelector.Items.Add(mfont);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多