Custom navbar/actionbar title font for both iOS and Android

LaKittehLaKitteh USMember

Hi! I've been googling around for about an hour now and have found some solution for setting a custom font for the navbar, however I haven't found any on how to set the font on android. Has anyone got an answer for this? I would like to use a font from a file, which I also haven't found how to do in iOS either.

Best Answers

Answers

  • Nadjib_BaitNadjib_Bait DZMember ✭✭✭✭

    In Android project, in MainActivity.cs, you can access ActionBar and modify it there (after initializing Xamarin Forms)

  • LaKittehLaKitteh USMember
    edited July 2015

    In what namespace do I find TypefaceSpan? Does it require a custom class?
    EDIT: I found it, however, where should i place the font file? Does it go in drawable like everything else or the root Resources folder? Thank you!

  • Nadjib_BaitNadjib_Bait DZMember ✭✭✭✭

    I think custom fonts should be placed in Assets directory, and set Build Action to AndroidAsset (if not set automatically by VS/XS) check documentation about Custom fonts in Xamarin Forms: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/

  • LaKittehLaKitteh USMember

    I am getting the following error when trying to compile with the code you provided above:
    /Users/aleorn/Dropbox/Arbete/Markit projekt/template/Droid/NavigationBarRenderer.cs(15,15): Error CS1540: Cannot access protected member "Android.Text.Style.TypefaceSpan.TypefaceSpan(System.IntPtr, Android.Runtime.JniHandleOwnership)" via a qualifier of type "Android.Text.Style.TypefaceSpan". The qualifier must be of type "template.Droid.NavigationBarRenderer" or derived from it (CS1540) (template.Droid) /Users/aleorn/Dropbox/Arbete/Markit projekt/template/Droid/NavigationBarRenderer.cs(15,15): Error CS0122: "Android.Text.Style.TypefaceSpan.TypefaceSpan(System.IntPtr, Android.Runtime.JniHandleOwnership)" is inaccessible due to its protection level (CS0122) (template.Droid)

  • AVINASHREDDY.8328AVINASHREDDY.8328 USMember ✭✭

    @nadjib said:
    var st = new SpannableString("Test");
    st.SetSpan(new TypefaceSpan(this, "CustomFont.otf"), 0, st.Length(), SpanTypes.ExclusiveExclusive);

      ActionBar.TitleFormatted = st;
    

    Where exactly I have to add these lines.

    I added this in main activity oncreate as below

    global::Xamarin.Forms.Forms.Init(this, bundle);
    var s = new SpannableString("Test")
    ......................
    ActionBar.TitleFormatted = st;

    It's breaking at **typeFace = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, typefaceName);
    saying forms.context is null. BTW, I am using CustomFontSpan mentioned by @DanielL .

    So I tried with SupportActionBar.TitleFormatted = s; which is not throwing error but my bar font style has not changed.

    Can you help me?

  • 15mgm1515mgm15 USMember ✭✭✭✭

    For some reason if I use this:

    var spannableString = new SpannableString(SupportActionBar.Title);
    spannableString.SetSpan(new CustomFontSpan("Branding-Medium.otf"), 0, spannableString.Length(), SpanTypes.ExclusiveExclusive);
    SupportActionBar.TitleFormatted = spannableString;
    

    Inside my MainActivity the font is still not being assigned.

    Any comments?

  • 15mgm1515mgm15 USMember ✭✭✭✭

    For Instance I came up using this package: https://github.com/mikescandy/Calligraphy-xamarin

Sign In or Register to comment.