如何使用Xamarin Forms更改导航页面标题的字体? [英] How can I change the font for the header of a Navigation page with Xamarin Forms?

查看:421
本文介绍了如何使用Xamarin Forms更改导航页面标题的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样更改字体颜色:

I can change the font color like this:

var homePage = new NavigationPage(new HomePage())
{ 
   Title = "Home",
   Icon = "ionicons_2_0_1_home_outline_25.png",
   BarTextColor = Color.Gray,
};

但是有一种方法可以更改标题的字体.我只想将其更改为iOS和Android平台.希望有人知道可以帮助我完成此操作的自定义渲染器代码.

But is there a way to change the font for the Title. I would like to change it for the iOS and Android platforms only. Hoping that someone knows of custom renderer code that can help me to do this.

推荐答案

您需要自定义渲染器,请参考

You need Custom Renderer , refer to this sample

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

        if (e.NewElement != null)
        {
            var att = new UITextAttributes();
            UIFont customFont = UIFont.FromName("Trashtalk", 20);
            UIFont systemFont = UIFont.SystemFontOfSize(20.0);
            UIFont systemBoldFont = UIFont.SystemFontOfSize(20.0 , FontAttributes.Bold);
            att.Font = font;
            UINavigationBar.Appearance.SetTitleTextAttributes(att);
        }
    }
}
}

Android

[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(CustomNavigationPageRenderer))]
namespace CustomFontsNavigationPage.Droid.Renderers
{
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
    private Android.Support.V7.Widget.Toolbar _toolbar;

    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();

        if (e.Child.GetType() == typeof(Android.Widget.TextView))
        {
            var textView = (Android.Widget.TextView)e.Child;
            var spaceFont = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, "Trashtalk.ttf");
            var systemFont = Typeface.DEFAULT;
            var systemBoldFont = Typeface.DEFAULT_BOLD;
            textView.Typeface = spaceFont;
            _toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
        }
    }
}
}

这篇关于如何使用Xamarin Forms更改导航页面标题的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆