.net 在 WPF 中使用自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3765647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:45:07  来源:igfitidea点击:

Using a custom font in WPF

.netwpf

提问by Prabath Yapa

I need my WPF app to use a true-type font for a different language. I have the font located in a folder called 'fonts' inside the project. The font I'm using is available for free download here

我需要我的 WPF 应用程序为不同的语言使用真正的字体。我的字体位于项目内名为“fonts”的文件夹中。我正在使用的字体可在此处免费下载

Since the font is installed in my system i first tried

由于字体安装在我的系统中,我第一次尝试

 FontFamily="FMBasuru"

I've read the post hereand tried doing (this is the exact markup I'm using including font name)

我已阅读此处的帖子并尝试这样做(这是我正在使用的确切标记,包括字体名称)

<Window.Resources>
        <Style x:Key="SinhalaFont">
            <Setter Property="TextElement.FontFamily" Value="fonts/#FMBasuru"/>
        </Style>
    </Window.Resources>

...

...

 <TextBlock  Style="{DynamicResource SinhalaFont}">r</TextBlock>

...

...

I made sure that I'm using the correct font name instead of the font filename. What could have I got wrong?

我确保我使用的是正确的字体名称而不是字体文件名。我可能做错了什么?

采纳答案by Mamta D

I tried your code with this

我用这个试过你的代码

  <Setter Property="TextElement.FontFamily" Value="fonts/#Arial Narrow Bold"/>

and it worked successfully.

它成功了。

Have you marked your font as 'Resource' in the Build Action? If you haven't, do that now and try your code again.

您是否在构建操作中将字体标记为“资源”?如果您还没有,请立即执行并再次尝试您的代码。

回答by Kishore Kumar

Updated:Create a folder name Fonts and copy the font which you want and change the BuildAction to Resource

更新:创建文件夹名称 Fonts 并复制您想要的字体并将 BuildAction 更改为 Resource

<Window.Resources>
    <FontFamily x:Key="test" >/Fonts/#Pirulen</FontFamily>
</Window.Resources>
<Grid>
    <TextBlock FontSize="25" HorizontalAlignment="Center" 
               FontFamily="{StaticResource test}">Kishore Kumar</TextBlock>
</Grid>

just refer this document

只需参考此文档

WPF - Add Custom Font

WPF - 添加自定义字体

回答by Tharindu

Without using style, you can simply add the font like this in the Window.xaml I included the font file inside the folder called "Fonts".

在不使用样式的情况下,您可以简单地在 Window.xaml 中添加这样的字体,我将字体文件包含在名为“Fonts”的文件夹中。

<Window

 FontFamily ="./Fonts/#Arial"

>

And if you want to use another font for specific label or text block you can override it like this. You should insert the font file into the Fonts folder.

如果您想为特定标签或文本块使用另一种字体,您可以像这样覆盖它。您应该将字体文件插入到 Fonts 文件夹中。

<TextBlock FontFamily = "./Fonts/#Tahoma" ></TextBlock>

回答by Raz Luvaton

To add a custom font to your application

向应用程序添加自定义字体

It's worked for me on Blend, I don't know about Visual Studio.

它在 Blend 上对我有用,我不知道 Visual Studio。

  1. In an open project in Expression Blend, under Files in the Project panel, right-click your project name, and then click Add Existing Item. enter image description here
  2. Browse to the custom font file (typically with a .ttf file name extension), select the custom font file so that it appears in the File text box, and then click Open. The custom font file is added to your application and appears under Files in the Project panel. enter image description here
  3. You can now embed the complete font or a subset the font in your application, and apply the font to text controls in your application.
  1. 在 Expression Blend 中打开的项目中,在“项目”面板的“文件”下,右键单击您的项目名称,然后单击“添加现有项目”。 在此处输入图片说明
  2. 浏览到自定义字体文件(通常带有 .ttf 文件扩展名),选择自定义字体文件,使其出现在“文件”文本框中,然后单击“打开”。自定义字体文件将添加到您的应用程序中,并显示在“项目”面板中的“文件”下。 在此处输入图片说明
  3. 您现在可以在应用程序中嵌入完整字体或字体子集,并将字体应用于应用程序中的文本控件。


The font will be in the font list

字体将在字体列表中

enter image description here

在此处输入图片说明

From - Add a custom font to your application

From -向您的应用程序添加自定义字体