【问题标题】:Why doesn't Font Awesome display for iOS apps using Xamarin Forms?为什么使用 Xamarin Forms 的 iOS 应用程序不显示 Font Awesome?
【发布时间】:2020-12-11 09:29:09
【问题描述】:

我正在尝试在我的标签页标签中添加字体很棒的图标。我遵循了我认为能够为 Xamarin.forms 应用程序配置显示字体真棒图标的正确步骤,但由于某种原因,图标显示为 android 应用程序而不是 iOS 应用程序。

我首先将字体 awesome .ttf 文件添加到 android Assets 文件夹和 iOS Resources 文件夹中:

接下来我更新了 iOS 的 Info.plist 文件,以便包含来自 Font Awesome 的 .ttf 文件:

然后我在 App.xaml 中配置了 Font Awesome 图标的使用:

最后我在他们的标签中添加了所需的图标:

【问题讨论】:

  • iOS 不使用字体系列的文件系统名称,它使用 postscript 系列名称(这些在文件中编码),这些名称可通过UIFont.FontNamesForFamilyName 获得,请查看代码我的答案的底部(stackoverflow.com/a/48191854/4984832),你可以临时。将它添加到您的 Xamarin.iOS 项目并从控制台/调试输出中获取您的名称。或者使用 macOS 系统应用程序:“Font Book”,在这里查看我的答案:stackoverflow.com/questions/45768488/…
  • 阅读本指南:devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms。您不需要将字体文件添加到平台项目中。然后删除应用数据并重试。
  • 请不要将代码或错误作为图片发布
  • @Saftpresse99 我之前尝试过遵循这些确切步骤,但它会导致在 android 项目的 MainActivity cs 文件中引发错误: System.TypeLoadException: 'Could not resolve type with token'

标签: android ios xamarin xamarin.forms font-awesome


【解决方案1】:

在 iOS 上,请使用“真实”字体名称而不是文件名。

您可以通过右键单击 Windows 上的 font file -> Properties -> Details -> Title 来检查。例如,我测试了一个文件名为Font Awesome 5 Free-Solid-900.otf 的自定义字体文件。如果将值设置为文件名则不起作用:

<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
    ...
    <On Platform="iOS" Value="Font Awesome 5 Free-Solid-900" />
</OnPlatform>

尝试获取自定义字体文件的倾斜度并删除“空格”。

指定如下值:

<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
    <On Platform="iOS" Value="FontAwesome5Free-Solid" />
</OnPlatform>

参考:Font Awesome not working in iOS

【讨论】:

    【解决方案2】:

    替换 App.xaml 中的 iOS 值。

    <OnPlatform x:TypeArguments="x:String" 
                    x:Key="FontAwesomeBrands">
                    <On Platform="iOS"
                    Value="FontAwesome5Brands-Regular" />
                </OnPlatform>
    
                <OnPlatform x:TypeArguments="x:String" 
                    x:Key="FontAwesomeSolid">
                    <On Platform="iOS" 
                    Value="FontAwesome5Free-Solid" />
                </OnPlatform>
    
                <OnPlatform x:TypeArguments="x:String" 
                    x:Key="FontAwesomeRegular">
                    <On Platform="iOS" 
                    Value="FontAwesome5Free-Regular" />
                </OnPlatform>
    

    要获得在 iOS 中使用的正确字体名称,请在 AppDelegate 中 FinishedLaunching 中使用以下代码。

    foreach (var family in UIFont.FamilyNames)
                {
                    System.Diagnostics.Debug.WriteLine($"{family}");
                    foreach (var names in UIFont.FontNamesForFamilyName(family))
                    {
                        System.Diagnostics.Debug.WriteLine($"{names}");
                    }
                }
    

    【讨论】:

    • 我试过了,但仍然没有看到显示字体真棒图标。此外,实现这个循环我无法输出名称。我正在通过 Visual Studio 2019 中的 macincloud 调试/运行 iOS 应用程序
    • @aqwright31 你能检查一下你的字体文件构建操作吗?是BundleResource吗?
    【解决方案3】:

    在 App.xaml 中设置这些值适用于 Android 和 iOS。

            <OnPlatform x:Key="FontAwesomeBrands"
                        x:TypeArguments="x:String">
                <On Platform="iOS"
                    Value="FontAwesome5Brands-Regular" />
                <On Platform="Android"
                    Value="FontAwesome5BrandsRegular400.otf#" />
            </OnPlatform>
    
            <OnPlatform x:Key="FontAwesomeSolid"
                        x:TypeArguments="x:String">
                <On Platform="iOS"
                    Value="FontAwesome5Free-Solid" />
                <On Platform="Android"
                    Value="FontAwesome5FreeSolid900.otf#" />
            </OnPlatform>
    
            <OnPlatform x:Key="FontAwesomeRegular"
                        x:TypeArguments="x:String">
                <On Platform="iOS"
                    Value="FontAwesome5Free-Regular" />
                <On Platform="Android"
                    Value="FontAwesome5FreeRegular400.otf#" />
            </OnPlatform>
    

    【讨论】:

      【解决方案4】:

      另一种方法是在主项目中将字体添加为Embedded Resource(抽象的字体,而不是*.iOS),并将以下内容添加到App.xaml.cs 的顶部(命名空间上方):

      [assembly: ExportFont("fa-solid-900.ttf", Alias = "FASolid")]
      [assembly: ExportFont("fa-regular-400.ttf", Alias = "FARegular")]
      [assembly: ExportFont("fa-brands-400.ttf", Alias = "FABrands")]
      

      这种方法适用于 Android 和 iOS。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-24
        • 2020-01-08
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多