簡體   English   中英

為什么不使用自定義字體? Xamarin.Forms.iOS

[英]Why is the custom font not being used? Xamarin.Forms.iOS

我現在正在使用自定義字體,目前使用的是一種名為:“Yellowtail-Regular.tff”的字體。 我在 iOS 文件夾中創建了一個名為Fonts的地圖,並在其中添加了 tff 文件。 之后,我轉到我的 Info.plist 並創建了一個Fonts provided by applicationFonts provided by application並在其中創建了一個名為Fonts/Yellowtail-Regular.ttf的新字符串。

當我現在嘗試像這樣使用它時:

<Label FontFamily = "Yellowtail" Text = "Testing my label" />

標准字體 (Helvetica Neue) 仍然存在。 我也試過:

<Label FontFamily = "Yellowtail-Regular" Text = "Testing my label" />

但結果是一樣的。

我錯過了一步還是我做錯了什么?

如果您使用來自http://www.1001freefonts.com/yellowtail.font 的Yellowtail-Regular.ttf ,那么iOS下的字體名稱將只是Yellowtail

每個平台都有不同的命名約定,因此使用Device.OnPlatform可能類似於:

public Label()
{
  FontFamily = Device.OnPlatform("Yellowtail", "Yellowtail-Regular", "/Assets/Yellowtail-Regular.ttf#Yellowtail-Regular");      
}

要確認您的 iOS 字體系列名稱,請在macOS使用Font Book打開您的.ttfmacOS / iOS將使用的名稱是應用程序標題欄中的名稱。

有時字體書會顯示帶有空格的名稱。

就我而言:字體名稱是: SFProDisplay-Ultralight 但字體書顯示名稱為“SF Pro Display Ultralight”。 由於此自定義字體在 iOS 中不起作用。

因此,您可以使用以下代碼找到正確的字體名稱並將其應用到 iOS 中。 在 AppDelegate 啟動方法中添加此代碼。 然后您可以在應用程序輸出中找到可行字體名稱的列表。

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

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

之后,您可以使用 xaml 來引用字體。

<OnPlatform x:Key="SFProDisplayUltraLight" x:TypeArguments="x:String">
    <On Platform="iOS" Value="SFProDisplay-Ultralight" />
    <On Platform="Android" Value="SF-Pro-Display-Ultralight.otf#SF Pro Display Ultralight" />
</OnPlatform>

然后將其應用於控件。

<Lable Text="Welcome" FontFamily="{StaticResource SFProDisplayUltraLight}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM