【问题标题】:Xamarin.Forms Custom FontXamarin.Forms 自定义字体
【发布时间】:2018-04-30 13:09:27
【问题描述】:

我正在尝试在我的 Xamarin 表单应用程序中实现自定义字体。字体是来自 Google 的 Quicksand-Regular。

清单: 字体已添加到 Fonts/、根目录和 Resources 文件夹的不同位置。 构建操作设置为 BundleResource,并且输出导向器已使用 Always Copy 和 Do Not Copy 进行了测试。 Plist 已修改为 UIAppFonts Quicksand-Regular.tff

字体的可用名称只是 Quicksand,但我尝试在应用程序中同时使用 Quicksand 和 Quicksand-Regular 来引用它。 我试图添加, 样式 TargetType="标签" Setter 属性="FontFamily" 值="流沙" 风格

应用范围的资源字典,以及直接引用标签上的字体,都只是指定 FontFamily 并将其称为平台特定的。 我看到的奇怪的事情是,在预览窗口中它工作正常,标签和按钮文本都被正确替换,但这在运行时不起作用。

我相信资源实际上并没有被正确地找到或复制到 iOS 项目中,但任何建议都将不胜感激。

【问题讨论】:

    标签: ios fonts xamarin.forms custom-font


    【解决方案1】:

    我只能列出我为在 iOS 上运行字体所做的工作:

    字体是 MaterialIcons-regular.ttf

    添加到资源文件夹

    构建操作 = BundleResources

    复制到输出目录 = 不要复制

    Label FontFamily = "Material Icons"(注意空格)

    将字体添加到 info.plist

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

    Here is a blog post 列出了 iOS 所需的步骤。它说复制到输出目录应该总是复制。不是我发现的,而是另一个可能的绊脚石。

    【讨论】:

    • 只是给了一个裂缝,没有骰子。当您说注意空格时,我的字体名称(实际名称,而不是文件名)是流沙。但是无论哪种方式。它仍在预览中显示,只是不在运行时显示。
    • 预览与运行时是什么意思?
    • 哦,特别是在 Visual Studio Community for Mac 中。更改会反映在 XAML 设计预览窗口中,但不会在应用运行时反映。
    • 忘记了 info.plist 步骤 - 已添加
    • 不确定 XAML 设计预览是否支持自定义字体。它可能只是使用常规字体,这就是为什么它看起来像是在预览但在运行时却没有的原因
    【解决方案2】:

    要使用自定义字体,您需要执行以下操作:

    ** 共享代码 **

    创建一个从元素派生的新类,您希望使用自定义字体显示,例如一个标签:

    CustomFontLabel.cs

    public class CustomFontLabel : Label
    {
    }
    

    是的,它基本上是一个空类。

    安卓 将字体(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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多