WPF同一字体族的多个字体文件

5
我有以下字体文件。
MyFont-Regular.tff
MyFont-Bold.tff
MyFont-Italic.tff
如何使用它们?
我可以进行以下操作,
<TextBlock 
FontFamily="/Fonts/MyFont/MyFont-Regular.ttf#My Font"
Text="This is my font"/>

如果我想使用斜体和粗体之类的样式,该怎么办?我不能声明My Font由多个包含字体样式的文件组成吗?
4个回答

4

这是完全可以做到的!您只需要指定字体文件所在的文件夹以及字体名称,而不是指定FontFamily="/Fonts/MyFont/MyFont-Regular.ttf#My Font"

FontFamily="/Fonts/MyFont/#My Font"

WPF首先检查该目录中的所有字体文件,并将它们加载到一个FontFamily中,如果字体名称与在#后指定的名称相匹配。

这样,您可以轻松地在资源中定义一个FontFamily,并通过指定属性FontWeightFontStyle来使用其样式:

<FontFamily x:Key="MyFont">/Fonts/MyFont/#My Font</FontFamily>

<!-- somewhere else: -->
<TextBlock
    Text="Hello World"
    FontFamily="{StaticResource MyFont}"
    FontWeight="Bold"/>

这将自动使用您在该文件夹中的TTF文件。

4
您不能直接实现该功能。但您可以将自定义字体打包到样式/资源中:
<App.Resources>
    <FontFamily x:Key="CustomRegular">/Fonts/MyFont/MyFont-Regular.ttf#My Font</FontFamily>
    <FontFamily x:Key="CustomBold">/Fonts/MyFont/MyFont-Bold.ttf#My Font</FontFamily>
    <FontFamily x:Key="CustomItalic">/Fonts/MyFont/MyFont-Italic.ttf#My Font</FontFamily>
</App.Resources>

然后像这样使用它:
<TextBlock FontFamily="{StaticResource CustomItalic}">Hello world</TextBlock>

需要将文本部分变成斜体吗?

<TextBlock FontFamily="{StaticResource CustomRegular}">
    <Run FontFamily="{StaticResource CustomItalic}">Hello</Run>
    <Run>World</Run>
</TextBlock>

祝你好运。


对我来说,这个关键部分是大多数示例都省略了TTF文件的名称,如果您的不同字体文件没有共享相同的内部名称,那么这是可以的。 - metal

4
这里有一种更好的做法:
  1. Add a /Fonts folder to your solution.
  2. Add the True Type Fonts (*.ttf) files to that order
  3. Include the files to the project
  4. Select the fonts and add them to the solution
  5. Set BuildAction: Resource and Copy To Output Directory: Do not copy. Your .csproj file should now should have a section like this one:

     <ItemGroup>
      <Resource Include="Fonts\NotoSans-Bold.ttf" />
      <Resource Include="Fonts\NotoSans-BoldItalic.ttf" />
      <Resource Include="Fonts\NotoSans-Italic.ttf" />
      <Resource Include="Fonts\NotoSans-Regular.ttf" />
      <Resource Include="Fonts\NotoSansSymbols-Regular.ttf" />
    </ItemGroup>
    
  6. In App.xaml add <FontFamily> Resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.

    <Applicaton ...>
    <Application.Resources>
        <FontFamily x:Key="NotoSans">pack://application:,,,/Fonts/#NotoSans</FontFamily>
        <FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#NotoSansSymbols</FontFamily>
    </Application.Resources>
    </Application>
    
  7. Apply your Fonts like this:

    <TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}" 
               FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
    
  8. You can also set the font imperatively:

    var uri = new Uri("pack://application:,,,/");
    myTextBlock.FontFamily = new FontFamily(uri, "./Fonts/#NotoSans");
    

参考资料


1

您可以在不使用任何“运行”命令的情况下完成此操作。

<TextBlock x:Name="UserInstructionsContent" Margin="0,30,0,0" FontFamily="Bahnschrift Light, Segoe MDL2 Assets" FontSize="24" Text="Hello &#x1F30E;">

XAML 输出: 你好


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接