为什么 Font Awesome 不显示 iOS 个使用 Xamarin Forms 的应用程序?
Why doesn't Font Awesome display for iOS apps using Xamarin Forms?
我正在尝试将超棒的字体图标添加到我的标签页选项卡中。我遵循了我认为能够为 Xamarin.forms 应用程序配置显示字体超棒图标的正确步骤,但由于某些原因,图标显示为 android 应用程序而不是 iOS应用
我首先将字体 awesome .ttf 文件添加到 android Assets 文件夹和 iOS Resources 文件夹:
接下来,我为 iOS 更新了 Info.plist 文件,以便包含来自 Font Awesome 的 .ttf 文件:
然后我在 App.xaml 中配置了 Font Awesome 图标的使用:
最后我将需要的图标添加到他们的标签中:
将您的 iOS 值替换为 App.xaml。
<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 中使用正确的字体名称,请在 FinishedLaunching
中的 AppDelegate 中使用以下代码。
foreach (var family in UIFont.FamilyNames)
{
System.Diagnostics.Debug.WriteLine($"{family}");
foreach (var names in UIFont.FontNamesForFamilyName(family))
{
System.Diagnostics.Debug.WriteLine($"{names}");
}
}
在 iOS 上,请使用 'Real' 字体名称而不是文件名。
您可以通过右键单击 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>
尝试获取自定义字体文件的倾斜度并删除 'space'。
指定如下值:
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
</OnPlatform>
设置这些值适用于 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>
另一种方法是在主项目中将字体添加为 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。
我正在尝试将超棒的字体图标添加到我的标签页选项卡中。我遵循了我认为能够为 Xamarin.forms 应用程序配置显示字体超棒图标的正确步骤,但由于某些原因,图标显示为 android 应用程序而不是 iOS应用
我首先将字体 awesome .ttf 文件添加到 android Assets 文件夹和 iOS Resources 文件夹:
接下来,我为 iOS 更新了 Info.plist 文件,以便包含来自 Font Awesome 的 .ttf 文件:
然后我在 App.xaml 中配置了 Font Awesome 图标的使用:
最后我将需要的图标添加到他们的标签中:
将您的 iOS 值替换为 App.xaml。
<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 中使用正确的字体名称,请在 FinishedLaunching
中的 AppDelegate 中使用以下代码。
foreach (var family in UIFont.FamilyNames)
{
System.Diagnostics.Debug.WriteLine($"{family}");
foreach (var names in UIFont.FontNamesForFamilyName(family))
{
System.Diagnostics.Debug.WriteLine($"{names}");
}
}
在 iOS 上,请使用 'Real' 字体名称而不是文件名。
您可以通过右键单击 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>
尝试获取自定义字体文件的倾斜度并删除 'space'。
指定如下值:
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
</OnPlatform>
设置这些值适用于 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>
另一种方法是在主项目中将字体添加为 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。