自定义字体不会通过自定义标签渲染器加载到Xamarin.iOS上

时间:2019-07-15 09:32:27

标签: xamarin xamarin.forms fonts xamarin.ios custom-renderer

我目前正在尝试通过为应用中的所有标签创建自定义渲染器来更改Xamarin.Forms应用的字体。似乎存在一个问题,在某些位置,文本更改为正确的字体,但是在大多数应用程序中,标签似乎丢失了文本并且根本不显示文本。

我正在创建一个Xamarin.Forms应用程序,该应用程序将对没有字体属性的文本使用Gotham-Book字体,对具有粗体字体属性的文本使用Gotham-Medium字体。两种字体均为.TTF,并且位于Resources文件夹内的Fonts文件夹中。

using System;
using System.ComponentModel;
using DemoApp.Controls;
using DemoApp.iOS.Renderers;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]

namespace DemoApp.iOS.Renderers
{
    public class CustomLabelRenderer : LabelRenderer
    {
        private bool _disposed;

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

                if (Control != null)
                {
                    var sizeToSet = GetNewFontSize();

                    if (Element.FontAttributes == FontAttributes.Bold)
                    {
                        Control.Font = UIFont.FromName("Gotham-Medium", (float)sizeToSet);
                    }
                    else
                    {
                        Control.Font = UIFont.FromName("Gotham-Book", (float)sizeToSet);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log
            }
        }
    }
}

我希望整个应用程序都能将应用程序更改为新的Gotham字体,但是大多数应用程序标签会丢失其文本且不显示任何内容。

0 个答案:

没有答案
相关问题