[英]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 上,請使用字體的“真實”名稱而不是文件名。
可以在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>
在 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 中使用正確的字體名稱,請在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}");
}
}
設置這些值適用於 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>
另一種方法是將 fonts 添加為主項目中的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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.