Custom label font with Xamarin Forms 1.3.0.6292

I'm using a custom font for many labels, and I'm using a custom renderer to make them work on Android as discussed here: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/.

Unfortunately, the upgrade to 1.3.0.6292 has broken this. The label displays correctly initially, as the typeface is set in the renderer's OnElementChanged method, but if the label text changes, the font reverts back to the default one. I've played around a bit with the FontFamily property that's been added, but I haven't found how/if I can make it work with a custom font on Android and have found no documentation on it.

Has anyone else run into this? Any solutions available?

Posts

  • 2BitNerd2BitNerd USMember ✭✭

    I'm also having an issue with this in Android. As long as the text is hard-coded in the label, my custom renderer does the job. If I have the label's TextProperty bound, the font never makes it to the final screen.

    This is a big one for us on our side. I'd greatly appreciate any feedback from Xamarin or workarounds from the community.

  • I've implemented a workaround by overriding OnElementPropertyChanged in the renderer and making sure there that the correct typeface is set on the font. This may not be the ideal solution but so far it seems to work well for me.

  • Sorry, meant to write "correct typeface is set on the TextView".

  • 2BitNerd2BitNerd USMember ✭✭

    @DavidJohnson.1675‌, would you mind posting that method to the forum? I think you're on to something.

  • 2BitNerd2BitNerd USMember ✭✭

    @DavidJohnson.1675, nevermind. I got it working for myself:

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    
        var fontFamily = ((NumberLabel)sender).FontFamily;
        if (!string.IsNullOrEmpty(fontFamily))
        {
            if (!fontFamily.Contains(".ttf")) fontFamily += ".ttf";
            var typeface = Typeface.CreateFromAsset(Forms.Context.Assets, fontFamily);
    
            var label = ((TextView)Control);
            label.Typeface = typeface;
        }
    }
    
  • Yes, that's basically the way I did it.

  • GaneshMJGaneshMJ USMember

    Could anyone of you guys please post the sample code that you have written.

  • TerryWestleyTerryWestley USUniversity ✭✭

    Thanks for the forum post and workaround. I've used it to overcome the same bug in Forms v1.3.2.6316.

    Did anyone file a bug report?

  • JulienGJulienG USMember

    Hello guys,
    Any news on this bug ??
    Someone of Xamarin can help us ?

    Because the workaround seems blocking for me (in terms of performance)...
    I have a scenario where I have to animate the label, so I have maybe hundred of 'PropertyChanged' events (with 'TranslationX', 'Opacity' and other properties...).
    Each time the font asset is recreated... And I can see on my device that it's not so smooth as with the default font...

    So ?
    Thanks !

  • JuanEstebanJuanEsteban COMember

    Hi,

    Take a look at this link:
    https://bugzilla.xamarin.com/show_bug.cgi?id=33666

    It works for me.

Sign In or Register to comment.