Forms中修改导航栏字体

Link :https://forums.xamarin.com/discussion/comment/357737#Comment_357737

需要Custom renderer

iOS

[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyRenderer ))]
namespace CustomFontsNavigationPage.iOS.Renderers
{
    public class MyRenderer  : NavigationRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                 var att = new UITextAttributes();
                UIFont customFont = UIFont.FromName("BradleyHandITCTT-Bold", 20);
                att.Font = customFont;
                UINavigationBar.Appearance.SetTitleTextAttributes(att);
            }
        }
    }
}

Android

[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyRenderer))]
namespace FormsApp.Droid
{
class MyRenderer : NavigationPageRenderer
{

    private Android.Support.V7.Widget.Toolbar _toolbar;
    private Context _context;

    public MyRenderer(Context context) : base(context)
    {
        _context = context;
    }

    public override void OnViewAdded(Android.Views.View child)
    {
        base.OnViewAdded(child);

        if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
        {
            _toolbar = (Android.Support.V7.Widget.Toolbar)child;
            _toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
        }
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (disposing)
        {
            _toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
        }
    }

    private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
    {
        var view = e.Child.GetType();


        Console.WriteLine(e.Child.GetType().ToString());
        if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
        {
            var textView = (Android.Support.V7.Widget.AppCompatTextView)e.Child;
            var spaceFont = Typeface.Create("sans-serif-condensed-light", TypefaceStyle.Bold);
            textView.Typeface = spaceFont;
            _toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
        }
    }
}
}

你可能感兴趣的:(Forms中修改导航栏字体)