簡體   English   中英

Xamarin.Forms自定義字體

[英]Xamarin.Forms Custom Font

我正在嘗試在Xamarin表單應用程序中實現自定義字體。 字體是Google的Quicksand-Regular。

清單:在不同位置將字體添加到Fonts /,根目錄和Resources文件夾。 構建操作設置為BundleResource,並且輸出導向器已通過始終復制和不復制進行了測試。 已通過UIAppFonts Quicksand-Regular.tff修改了Plist

字體的可用名稱只是流沙,但我嘗試在應用中同時使用流沙和常規流沙來引用它。 我嘗試添加樣式TargetType =“ Label” Setter Property =“ FontFamily” Value =“ Quicksand”樣式

對於應用程序范圍的資源字典,以及直接引用標簽上的字體,只需指定FontFamily並將其作為特定於平台的引用即可。 我看到的奇怪的是,在預覽窗口中,它工作正常,標簽和按鈕文本均已正確替換,但這在運行時不起作用。

我相信,實際上並沒有正確找到該資源或將其復制到iOS項目中,但是任何建議都將不勝感激。

我只能列出為在iOS上使用字體而做的事情:

字體為MaterialIcons-regular.ttf

已添加到資源文件夾

生成操作= BundleResources

復制到輸出目錄=不復制

標簽FontFamily =“材料圖標”(注意空格)

將字體添加到info.plist

<key>UIAppFonts</key>
<array>
    <string>MaterialIcons-Regular.ttf</string>
</array>

這是一篇博客文章,列出了iOS上所需的步驟。 它說復制到輸出目錄應該總是復制。 不是我發現的,而是另一個可能的絆腳石。

為了使用自定義字體,您需要執行以下操作:

**共享代碼**

創建一個新的類,該類派生自要使用自定義字體顯示的元素,例如Label:

CustomFontLabel.cs

public class CustomFontLabel : Label
{
}

是的,它基本上是一個空類。

Android將字體(ttf文件)添加到應用程序的資產(不是資源!),並將構建操作設置為“ AndroidAsset”。

現在在您的android項目中創建一個自定義渲染器:

CustomFontRenderer.cs

[assembly: ExportRenderer(typeof(CustomFontLabel), typeof(CustomFontRenderer))]
namespace MyNamespace.Droid.Renderer.Elements
{
    public class CustomFontRenderer: LabelRenderer
    {
        public CustomFontRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            TextView label = (TextView)Control;

            if (e.NewElement?.FontFamily != null)
            {
                Typeface font = null;
                // the try-catch block will ensure the element is at least rendered with default 
                // system font in Xamarin Previewer instead of crashing the view
                try
                {
                    font = Typeface.CreateFromAsset(AndroidApp.Application.Context.Assets, e.NewElement.FontFamily);
                }
                catch (Exception)
                {
                    font = Typeface.Default;
                }
                label.Typeface = font;
            }
        }
    }

的iOS

將字體添加到資源文件夾,並確保將構建操作設置為“ BundleResource”。

接下來,將字體添加到info.plist中,例如:

  <key>UIAppFonts</key>
  <array>
    <string>fontawesome.ttf</string>
    <string>OpenSans-Light.ttf</string>
    <string>OpenSans-Bold.ttf</string>
    <string>OpenSans-LightItalic.ttf</string>
    <string>OpenSans-Italic.ttf</string>
    <string>OpenSans-Regular.ttf</string>
    <string>Lato-Bold.ttf</string>
    <string>Lato-Regular.ttf</string>
  </array>

現在添加一個自定義渲染器:

CustomFontRenderer.cs

[assembly: ExportRenderer(typeof(CustomFontLabel), typeof(CustomFontRenderer))]
namespace MyNamespace.iOS.Renderer.Elements
{
    public class CustomFontRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null && e.NewElement.FontFamily != null)
            {
                e.NewElement.FontFamily = e.NewElement.FontFamily.Replace(".ttf", "");
            }
        }
    }
}

現在回到表單視圖,您可以插入自定義標簽:

<elements:CustomFontLabel Text="A simple label using font family 'Lato'" FontFamily="Lato-Bold.ttf" />

暫無
暫無

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

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