There are two ways of doing this: One way is to package the fonts inside the application. The other way is to have the fonts in a folder. The difference is mostly the URI you need to load the files with.
Package with Application
-
Add a
/Fontsfolder to your solution. -
Add the True Type Fonts (
*.ttf) files to that folder -
Include the files to the project
-
Select the fonts and add them to the solution
-
Set
BuildAction: ResourceandCopy To Output Directory: Do not copy. Your.csprojfile should now 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> -
In
App.xamladd<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/#Noto Sans</FontFamily> <FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#Noto Sans Symbols</FontFamily> </Application.Resources> </Application> -
Apply your fonts like this:
<TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}" FontSize="10.0" FontStyle="Normal" FontWeight="Regular" /> -
You can also set the font imperatively:
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#Noto Sans");
Copy to Output Directory
-
Add a
/Fontsfolder to your solution. -
Add the True Type Fonts (
*.ttf) files to that order -
Include the files to the project
-
Select the fonts and add them to the solution
-
Set
BuildAction: ContentandCopy To Output Directory: Copy if newerorCopy always. Your.csprojfile should now have a section like this one:<ItemGroup> <Content Include="Fonts\NotoSans-Bold.ttf"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="Fonts\NotoSans-BoldItalic.ttf"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="Fonts\NotoSans-Italic.ttf"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="Fonts\NotoSans-Regular.ttf"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="Fonts\NotoSansSymbols-Regular.ttf"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> -
In
App.xamladd<FontFamily>resources. It should look like in the following code sample.<Applicaton ...> <Application.Resources> <FontFamily x:Key="NotoSansRegular">./Fonts/NotoSans-Regular.ttf#Noto Sans</FontFamily> <FontFamily x:Key="NotoSansItalic">./Fonts/NotoSans-Italic.ttf#Noto Sans</FontFamily> <FontFamily x:Key="NotoSansBold">./Fonts/NotoSans-Bold.ttf#Noto Sans</FontFamily> <FontFamily x:Key="NotoSansBoldItalic">./Fonts/NotoSans-BoldItalic.ttf#Noto Sans</FontFamily> <FontFamily x:Key="NotoSansSymbols">./Fonts/NotoSans-Regular.ttf#Noto Sans Symbols</FontFamily> </Application.Resources> </Application> -
Apply your fonts like this:
<TextBlock Text="foobar" FontFamily="{StaticResource NotoSansRegular}" FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
References
- MSDN: Packaging Fonts with Applications