【问题标题】:Xamarin.Forms custom font not picked up on iOS device when FontAttributes specified指定 FontAttributes 时未在 iOS 设备上拾取 Xamarin.Forms 自定义字体
【发布时间】:2018-01-21 08:50:09
【问题描述】:

我在我们的应用程序中使用自定义字体,并发现如果我使用该字体为标签样式设置 FontAttributes="Bold",它可以在 iOS 模拟器上运行,但不能在真实设备上运行。

采取的步骤: 1. 将字体 Effra_Std_Reg.ttf 添加到 resources 文件夹。 2. 将此添加到Info.plist 中的应用程序提供的字体 元素并设置其构建操作。 3. 在 App.XAML 中创建样式。 4. 将这些样式添加到我页面上的 XAML 中。

All 对于没有设置 FontAttributes 的标签,上述内容可以正常工作。只要我在样式或标签本身上设置 FontAttributes="Bold",就会使用系统字体。请注意这适用于模拟器

我知道 FontFamily 是 ttf 文件中定义的字体 Name,而不是 ttf 文件名。

这在运行 iOS 10.3.3 的 iPad 上。

我的 App.XAML 是:

<?xml version="1.0" encoding="utf-8" ?>
<Application x:Class="FontTest.App"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Label">
                <Setter Property="FontSize" Value="30" />
            </Style>

            <Style x:Key="PlainLabelStyle" TargetType="Label">
                <Setter Property="FontFamily">
                    <Setter.Value>
                        <OnPlatform x:TypeArguments="x:String"
                                    Android="Effra_Std_Rg.ttf#Effra"
                                    iOS="Effra" />
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="BoldLabelStyle" TargetType="Label">
                <Setter Property="FontFamily">
                    <Setter.Value>
                        <OnPlatform x:TypeArguments="x:String"
                                    Android="Effra_Std_Bd.ttf#Effra"
                                    iOS="Effra" />
                    </Setter.Value>
                </Setter>
               <Setter Property="FontAttributes" Value="Bold" />
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>

而我的页面 XAML(摘录)是:

 <Label Grid.Row="4"
               Grid.Column="1"
               Style="{StaticResource BoldLabelStyle}"
               Text="QWERTYUIOPASDFGHJKLZXCVBNM" />

【问题讨论】:

    标签: ios fonts xamarin.forms


    【解决方案1】:

    为了让它在设备上运行,我必须包含粗体字体文件 (Effra_Std_Bd.ttf),并指定一个 FontFamily 为“Effra-Bold”。

    所以看来 FontFamily 需要是 NameStyle

    奇怪的是它在模拟器上工作。

    【讨论】: