【问题标题】:I can't get my custom fonts to work with Xamarin.Forms我无法让我的自定义字体与 Xamarin.Forms 一起使用
【发布时间】:2019-05-27 14:54:17
【问题描述】:

我创建了这个资源:

<?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 的资源文件中

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

【问题讨论】:

标签: xamarin xamarin.forms


【解决方案1】:

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

  1. 如果您在资源文件中添加了名为 Camber_Medium_Regular.ttf 的字体文件,并且 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>

【讨论】:

    最近更新 更多