locked
Custom font for Label RRS feed

  • Question

  • User64262 posted

    I want a custom font for a label in my app. I followed the guide ( http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/#UsingaCustom_Font ) and created a class named KomikaLabel in my shared project. The font is in Assets/fonts in the android project. Then I added this code to the android project:

        [assembly: ExportRenderer (typeof (DonnerWetter.KomikaLabel), typeof (DonnerWetter.Android.KomikaRenderer))]
        namespace DonnerWetter.Android {
            public class KomikaRenderer : LabelRenderer {
                protected override void OnElementChanged (ElementChangedEventArgs<Label> e) {
                    base.OnElementChanged (e);
                    var label = (TextView)Control;
                    Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "KOMTXT__.ttf");
                    label.Typeface = font;
                }
            }
        }
    

    I tested it on two different devices but I just get a runtime exception "native typeface cannot be made". Did I miss something?

    Monday, August 11, 2014 1:21 PM

All replies

  • User3466 posted

    There is an ExtendedLabel control in the Xamarin Forms Labs project that will allow you to set the font.

    https://github.com/XForms/Xamarin-Forms-Labs

    Monday, August 11, 2014 2:04 PM
  • User14 posted

    @Superwayne? did you get the example from the Working with Fonts doc working before you tried with a different font?

    Note that the doc specifies:

    create a Typeface instance that references a custom font file that has been added to the Assets directory of the application

    You said you put the file in Assets/fonts - have you tried moving the font to the Assets folder?

    Monday, August 11, 2014 4:29 PM
  • User64262 posted

    Thanks Craig, the folder was the problem. Other sources said fonts need to be in Assets/fonts for android..

    Thank you too Mitch, I will have a look at Forms Labs.

    Tuesday, August 12, 2014 7:06 AM