首页 > 解决方案 > UWP IoT 如何添加自定义字体

问题描述

我无法添加自定义字体。我按照此处参考此页面的步骤进行操作,但未使用该字体。

我已将字体添加到我的项目中

在此处输入图像描述

确实出现在我的设计师视图中

在此处输入图像描述

在 Windows 中打开的字体(验证字体名称):

在此处输入图像描述

但是当我提到它们时,它们仍然没有在我的应用程序中使用。我尝试了以下语法

<TextBlock 
    Grid.Column="1" 
    Text="{x:Bind Text}"
    FontFamily="ms-appx:///Assets/Delphi-Soleto_Lt.ttf#Delphi-Soleto Light"/>

或者当我在设计师中选择字体时设计师填写它

<TextBlock 
    Grid.Column="1" 
    Text="{x:Bind Text}"
    FontFamily="Delphi-Soleto Light"/>

或如本网站建议的那样

<TextBlock 
    Grid.Column="1" 
    Text="{x:Bind Text}"
    FontFamily="/Assets/Delphi-Soleto_Lt.ttf#Delphi-Soleto Light"/>

非常感谢您的帮助...!

将 GIT 存储库上传到:

https://github.com/basprins/CustomFontWinIot

我正在尝试在以下位置使用 Delphi 字体BulletText.xaml

    <TextBlock 
        Grid.Column="1" 
        Margin="0, 13, 0, 0"
        Style="{StaticResource Normal}" 
        Text="{x:Bind Text}"
        FontFamily="{StaticResource DefaultFont}"/>

我将BulletText控件添加到主页,当应用程序启动时该控件将立即可见。

使用空白应用程序更新 git repo,该应用程序引用了不会显示的字体

在我的电脑上,我没有字体安装到 Windows 中。我刚刚将它们添加到项目中。代码见github页面。设计器视图截图

在此处输入图像描述

标签: c#fontsuwp

解决方案


您不应使用FontFamily属性中的字体粗细索引来引用字体文件。如果需要设置字体粗细,可以设置TextBlock的FontWeight属性。试试这个:

<StackPanel>
    <!--Don't add "Light" in the FontFamily-->
    <TextBlock FontFamily="ms-appx:///Assets/Delphi-Soleto_Lt.ttf#Delphi-Soleto"
               Text="Hello world, this is my test"/>
    <!--Don't add "Thin" in the FontFamily-->
    <TextBlock FontFamily="ms-appx:///Assets/Delphi-Soleto_Th.ttf#Delphi-Soleto"
               Text="Hello world, this is my test"/>
    <!--This is the default font family, set the FontWeight property-->
    <TextBlock Text="Hello world, this is my test" FontWeight="Normal"/>
</StackPanel>

这是效果:

在此处输入图像描述


推荐阅读