How to change the font family of master details page header and its content page

AswathyAswathy Member ✭✭✭

Hi,
I want to change the font family of the master details page header and it's content page in xamarin forms. How can I do this

Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited December 2018

    You need 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;
                }
            }
        }
    }
    
  • AswathyAswathy Member ✭✭✭

    @ColeX I tried this, but this doesn't work in master details page header

  • ColeXColeX Member, Xamarin Team Xamurai

    Do you mean the page title ?

  • AswathyAswathy Member ✭✭✭

    @ColeX Yes

  • ColeXColeX Member, Xamarin Team Xamurai

    I have update my code , please check it , this is the test on my side .

    iOS

    Android

Sign In or Register to comment.