我无法将自定义字体与Xamarin.Forms一起使用

时间:2019-05-25 13:57:35

标签: xamarin xamarin.forms

我创建了此资源:

<?xml version="1.0" encoding="utf-8"?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CustomFrameApp.App">
    <Application.Resources>
        <ResourceDictionary>
            <OnPlatform x:TypeArguments="x:String" x:Key="NewFont">
                <On Platform="iOS" Value="Camber_Medium_Regular" />
            </OnPlatform>
        </ResourceDictionary>
    </Application.Resources> </Application>

已添加到info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>UIAppFonts</key>
    <array>
        <string>Camber_Medium_Regular.otf</string>
    </array>
</dict>
</plist>

并用于我的XAML

<Label FontSize="12px" Text="ABCD" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Center" FontFamily="{StaticResource NewFont}" />

但是字体仍然没有使用

我按照建议将它们添加为捆绑资源,并且它们位于iOS的资源文件中

有人知道什么地方可能出问题吗?

1 个答案:

答案 0 :(得分:1)

根据Fonts in Xamarin.Forms,请确认三件事:

  1. 如果您在资源文件中添加了名为Camber_Medium_Regular.ttf的字体文件tat,并且 Build Action:BundleResource

2。像这样更新Info.plist:

    <key>UIAppFonts</key>
<array>
    <string>Camber_Medium_Regular.ttf</string>
</array>

3。按名称引用

<OnPlatform x:TypeArguments="x:String">
                <On Platform="iOS" Value="Camber_Medium_Regular" />
                <On Platform="Android" Value="Lobster-Regular.ttf#Lobster-Regular" />
                <On Platform="UWP" Value="Assets/Fonts/Lobster-Regular.ttf#Lobster" />
            </OnPlatform>