首页 > 解决方案 > 我无法让我的自定义字体与 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 的资源文件中

有谁知道可能出了什么问题?

标签: xamarinxamarin.forms

解决方案


根据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>

推荐阅读