首页 > 解决方案 > Font Awesome 仅在 xamarin 上显示 ASCII 字符

问题描述

TL;博士

Font Awesome 5 Solid 仅在我的 xamarin 应用程序中显示 ASCII 字符,但不显示特殊字体真棒字符。

我的情况:

我已将Font Awesome 5 Solid字体添加到我的 xamarin 项目中以使用大麻图标

这些是我添加字体的步骤:

  1. 将 .otf 文件添加到 AndroidAssets文件夹

  2. 在我的 App.xaml 中为每个平台定义字体名称,如下所示:

    <OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
         <On Platform="Android" 
             Value="FontAwesome5Solid.otf" />
         <On Platform="iOS" 
             Value="FontAwesome5Free-Solid" />
         <On Platform="UWP" 
             Value="/Assets/FontAwesome5Solid.otf" />
    </OnPlatform>
    

然后我尝试xaml <span>像这样访问字体:

<Span Text="&#xf55f;"FontFamily="{StaticResource FontAwesomeSolid}" />

作为参考,请参阅下面的整个代码块。

  <Label.FormattedText>
       <FormattedString>
            <Span Text="&#xf55f;"FontFamily="{StaticResource FontAwesomeSolid}" />
            <Span Text="{Binding Source={x:Static model:User.Rating}, StringFormat='{0}'}"
                  Style="{StaticResource MenuLabel}">
                  <Span.GestureRecognizers>
                       <TapGestureRecognizer Command="{Binding TapCommand}" />
                  </Span.GestureRecognizers>
            </Span>
       </FormattedString>
  </Label.FormattedText>

问题:

A-Z 0-9当我使用 HTML 实体代码和特殊 ASCII 字符(Å甚至可以正常工作)访问它们时,正常的 ASCII 字母可以正常工作。如果我使用Text="&#x49;",这会导致标签将字母1作为其文本,正如预期的那样。

Font Awesome 5 Solid 的大麻符号是f55f,但是当我将其包含Text="#xf55f;"在标签中时,我得到了带有十字的小盒子(字符未找到错误)。

标签: xamarinfontsfont-awesome

解决方案


像这样更改 App.xaml 怎么样:

<OnPlatform x:TypeArguments="x:String" 
            x:Key="FontAwesomeSolid"> 
  <On Platform="Android" 
      Value="FontAwesome5Solid.otf#Regular" /> 
  <On Platform="iOS" 
      Value="FontAwesome5Free-Solid" /> 
  <On Platform="UWP" 
      Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" /> 
</OnPlatform> 

参考:https ://medium.com/@tsjdevapps/use-fontawesome-in-a-xamarin-forms-app-2edf25311db4


推荐阅读