WPF - Add Custom Font

纵饮孤独 提交于 2019-11-27 20:44:50

问题


I'm trying to add a custom font as a resource in my application. I have a "CustomFont" directory in the application and all the fonts inside of it are set to "Resource"

<Window.Resources>
    <Style x:Key="Gotham-XLight">
        <Setter Property="TextElement.FontFamily" 
                Value="/CustomFonts;Component/#Gotham-XLight" />
    </Style>
</Window.Resources>

And then on my TextBlock I have this: (inside a grid)

<TextBlock x:Name="TimeTextBlock" Style="{DynamicResource Gotham-XLight}" 
           TextAlignment="Center" FontSize="25" FontWeight="Bold" 
           Foreground="White" Text="TextBlockTimer" 
           Margin="105,242.974,0,226.975" HorizontalAlignment="Left" 
           Width="221.919" />

But I'm not seeing my font as people say. Am I doing something wrong?


回答1:


You may want to check the name of the font, you need to specify the name of the font not the name of the file.

Double click on the font file and it should show a "Font name:" that's what you want to make sure is specified in your style.




回答2:


Try this

<Window.Resources>
    <Style x:Key="Gotham-XLight">
        <Setter Property="TextElement.FontFamily" Value="CustomFonts/#Gotham-XLight" />
    </Style>
</Window.Resources>

Also, if you are not planning on changing the style at runtime {StaticResource Gotham-XLight} will be much more performant.




回答3:


In xaml I did it like this:

    <Button Grid.Column="1" Grid.RowSpan="2" Name="start" Margin="5" Click="start_Click">
        <TextBlock Name="test" FontFamily="pack://application:,,,/Y_Yoga;Component/Resources/#FontAwesome">&#xF04B;</TextBlock>
    </Button>

However, I don't know if #FontAwesome is font's embedded name or is it the result that I renamed the .ttf file.

Hope to be helpful!



来源:https://stackoverflow.com/questions/358501/wpf-add-custom-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!