首页 > 解决方案 > 如何在 StaticResource 中设置字体?

问题描述

我正在尝试在 App.xaml 中定义 FontAwesome fontFamily,以便可以在我的代码中使用 staticresource。

直接在页面中设置 fontFamily 是有效的。问题是当我尝试从 StaticResource 获取此值时,我显示了一个空白页面。

应用程序.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
             x:Class="RedPrairie.App">
    <Application.Resources>

        <!-- Application resource dictionary -->
        <ResourceDictionary>
            <!-- Font Awesome fonts path per plateform -->
            <OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String"  Android="FontAwesome5Brands.otf#Font Awesome 5 Brands Regular"  iOS="Font Awesome 5 Free" />
            <OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String" Android="FontAwesome5Solid.otf#Font Awesome 5 Free Solid"  iOS="Font Awesome 5 Free" />
            <OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String" Android="FontAwesome5Regular.otf#Font Awesome 5 Free Regular"  iOS="Font Awesome 5 Free" />
        </ResourceDictionary>
    </Application.Resources>
</prism:PrismApplication>

我的页面:


            <Label Text="{x:Static model:Icon.far_user}" 
                    FontFamily="{StaticResource FontAwesomeRegular}"
                       FontSize="Large"
                       TextColor="SkyBlue"
                       VerticalOptions="Center">

            </Label>

工作页面:

<Label Text="{x:Static model:Icon.far_user}" 
                       FontSize="Large"
                       TextColor="SkyBlue"
                       VerticalOptions="Center">
                <Label.FontFamily>
                    <OnPlatform 
                     x:TypeArguments="x:String"
                     Android="FontAwesome5Regular.otf#Font Awesome 5 Free Regular" 
                     iOS="Font Awesome 5 Free"  />
                                    </Label.FontFamily>

            </Label>

另一个工作页面:

<Label Text="&#xf007;" 
                       FontSize="Large"
                       TextColor="SkyBlue"
                       VerticalOptions="Center">
                <Label.FontFamily>
                    <OnPlatform 
                     x:TypeArguments="x:String"
                     Android="FontAwesome5Regular.otf#Font Awesome 5 Free Regular" 
                     iOS="Font Awesome 5 Free"  />
                                    </Label.FontFamily>

            </Label>

类图标的内容:

  public static class Icon
    {
        /*Regular Icons - prefixed with FAR_*/

        public static string far_address_book = "\uf2b9";
        public static string far_address_card = "\uf2bb";
        public static string far_angry = "\uf556";
        public static string far_arrow_alt_circle_down = "\uf358";
        public static string far_arrow_alt_circle_left = "\uf359";
        public static string far_arrow_alt_circle_right = "\uf35a";
        public static string far_arrow_alt_circle_up = "\uf35b";
        public static string far_user = "\uf007";
...
}

我在这里做错了什么?

标签: xamlxamarin.forms

解决方案


简化字体文件名并使用如下所示的字体名称。

似乎 Android 对字体名称并不严格,但是,如果字体文件名不正确,则会抛出 RuntimeException: 'Font asset not found '。

在 iOS 上,如果UIAppFonts数组键中的字体文件名不正确Info.plist,或者字体名称不正确,则字形/文本在框架内显示为问号。

在 UWP 上,如果字体文件名或字体系列名称不正确,字形/文本仅显示为框架。

项目中的字体文件位置和内容类型:

iOS:字体文件位置:Resources文件夹。内容类型:BundleResource

Android:字体文件位置:Assets文件夹。内容类型:AndroidAsset

UWP:字体文件位置:Assets文件夹。内容类型:Content

ResourceDictionary

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

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

  <OnPlatform x:TypeArguments="x:String" x:Key="FaBrands">
    <On Platform="iOS" Value="FontAwesome5Brands-Regular" />
    <On Platform="Android" Value="FontAwesome5Brands.otf#Font Awesome Brands" />
    <On Platform="UWP" Value="Assets/FontAwesome5 Brands.otf#Font Awesome 5 Brands" />
  </OnPlatform>

示例Label

<Label Text="&#xf2b9;" TextColor="Accent" FontFamily="{StaticResource FaRegular}" />
<Label Text="&#xf641;" TextColor="Accent" FontFamily="{StaticResource FaSolid}" />    
<Label Text="&#xf26e;" TextColor="Accent" FontFamily="{StaticResource FaBrands}" />

对于 iOS,使用 Open With > XML 编辑器添加以下内容Info.plist

<key>UIAppFonts</key>
<array>
<string>FontAwesome5Brands.otf</string>
<string>FontAwesome5Regular.otf</string>
<string>FontAwesome5Solid.otf</string>
</array>

推荐阅读