如何添加图标字体?

时间:2023-01-13 12:40:48

I'm trying to include icon font in my wpf application, but my icon never appears and only displays a rectangle.

我试图在我的wpf应用程序中包含图标字体,但我的图标永远不会出现,只显示一个矩形。

Projet structure :

Projet结构:

MyProject/
  -- Fonts/
      -- myFont.ttf
  -- MyWindow.xaml

MyWindow.xaml

<Label Text="&#xea21;" FontFamily="/Fonts/#myfont" />

"myfont" is the name of the font (the one I see after "Font name" when I double click on the file). The file's Build Action is set as "Resource".

“myfont”是字体的名称(当我双击文件时,我在“字体名称”后面看到的字体)。文件的Build Action设置为“Resource”。

I also tried the following :

我也尝试过以下方法:

Using style

<Window.Resources>
    <Style x:key="MyFont">
        <Setter property="TextElement.FontFamily"/Fonts/#myfont" />
    </Style>
</Window.Resources>

<Label Text="&#xea21;" Style="{StaticResource MyFont}" />

Using pack URI

使用pack URI

<Window.Resources>
    <Style x:key="MyFont">
        <Setter property="TextElement.FontFamily" value="v/Fonts/#myfont" />
    </Style>
</Window.Resources>

<Label Text="&#xea21;" Style="{StaticResource MyFont}" />

None of them work. Moreover, the last one (with pack URI) generates an error. Something like an "index out of range error" (I have VS in French so I can't tell what the message is in English).

他们都没有工作。而且,最后一个(带有包URI)会产生错误。像“索引超出范围错误”之类的东西(我的法语是VS,所以我不知道这个消息是用英语写的)。

Already tried to find a solution on the internet, but all I could find was using pack URI, or checking that the name of the font is correct or that Build Action is set to "Resource".

已经尝试在互联网上找到解决方案,但我发现只能使用包URI,或者检查字体名称是否正确或者Build Action是否设置为“Resource”。

Thank you.

EDIT

I precise that I don't want to use Blend. I would like to manage to do it directly in code. Thank you.

我确切地说我不想使用Blend。我想设法直接在代码中完成它。谢谢。

1 个解决方案

#1


Wpf supports embedded fonts - the easiest way to set these up is to assign a font to a visual element, such as a label via the font family property. I normally use blend (part of visual studio) which displays a font manager button. You can then embed the font in your application from there.

Wpf支持嵌入字体 - 设置它们的最简单方法是通过字体系列属性为可视元素指定字体,例如标签。我通常使用混合(visual studio的一部分)来显示字体管理器按钮。然后,您可以从那里将字体嵌入到应用程序中。

Otherwise MSDN have a page which details other options. https://msdn.microsoft.com/en-us/library/ms753303%28v=vs.110%29.aspx

否则,MSDN会有一个详细说明其他选项的页面。 https://msdn.microsoft.com/en-us/library/ms753303%28v=vs.110%29.aspx

If you edit the .csproj file for your project and add the item group...

如果编辑项目的.csproj文件并添加项目组...

  <ItemGroup>
    <BlendEmbeddedFont Include="Fonts\ahronbd.ttf">
      <IsSystemFont>True</IsSystemFont>
      <All>True</All>
      <AutoFill>True</AutoFill>
      <Uppercase>True</Uppercase>
      <Lowercase>True</Lowercase>
      <Numbers>True</Numbers>
      <Punctuation>True</Punctuation>
    </BlendEmbeddedFont>
  </ItemGroup>

You will need a Fonts folder with that .ttf file in there as part of your solution folder. This isn't pretty, but it will work without relying on additional tools.

您将需要一个带有.ttf文件的Fonts文件夹作为解决方案文件夹的一部分。这不是很好,但它可以在不依赖其他工具的情况下工作。

#1


Wpf supports embedded fonts - the easiest way to set these up is to assign a font to a visual element, such as a label via the font family property. I normally use blend (part of visual studio) which displays a font manager button. You can then embed the font in your application from there.

Wpf支持嵌入字体 - 设置它们的最简单方法是通过字体系列属性为可视元素指定字体,例如标签。我通常使用混合(visual studio的一部分)来显示字体管理器按钮。然后,您可以从那里将字体嵌入到应用程序中。

Otherwise MSDN have a page which details other options. https://msdn.microsoft.com/en-us/library/ms753303%28v=vs.110%29.aspx

否则,MSDN会有一个详细说明其他选项的页面。 https://msdn.microsoft.com/en-us/library/ms753303%28v=vs.110%29.aspx

If you edit the .csproj file for your project and add the item group...

如果编辑项目的.csproj文件并添加项目组...

  <ItemGroup>
    <BlendEmbeddedFont Include="Fonts\ahronbd.ttf">
      <IsSystemFont>True</IsSystemFont>
      <All>True</All>
      <AutoFill>True</AutoFill>
      <Uppercase>True</Uppercase>
      <Lowercase>True</Lowercase>
      <Numbers>True</Numbers>
      <Punctuation>True</Punctuation>
    </BlendEmbeddedFont>
  </ItemGroup>

You will need a Fonts folder with that .ttf file in there as part of your solution folder. This isn't pretty, but it will work without relying on additional tools.

您将需要一个带有.ttf文件的Fonts文件夹作为解决方案文件夹的一部分。这不是很好,但它可以在不依赖其他工具的情况下工作。