Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Custom Font Renderer Nullified when ViewCell Leaves Screen

$
0
0

This is confirmed for Android, haven't had time to test iOS. The screenshot illustrates the issue, I also provide my code. Essentially, when a custom renderer-font with a non-system font exits a listview "view" (aka is scrolled up or down out of view), it changes back to the normal font instead of the custom font.

GLabel.cs

class GLabel : Label {

    public static readonly BindableProperty FontFamilyProperty =
      BindableProperty.Create<GLabel, string> (p => p.Text, string.Empty);

    public string FontFamily
    {
        get { return (string) GetValue(FontFamilyProperty); }
        set { SetValue(FontFamilyProperty, value); }
    }

}

GLabelRenderer.cs

public class GLabelRenderer : LabelRenderer {

    protected override void OnElementChanged (ElementChangedEventArgs<Label> e) {

        base.OnElementChanged (e);

        if (e.OldElement != null || Element == null) return;

        var gl = (GLabel)Element;
        var fontFamily = gl.FontFamily;

        if (!string.IsNullOrEmpty(fontFamily))
        {
            if (!fontFamily.Contains(".ttf")) fontFamily += ".ttf";
            var typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "fonts/" + fontFamily);

            Control.Typeface = typeface;
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName != "Text") return;

        var gl = (GLabel)Element;

        var fontFamily = gl.FontFamily;
        if (!fontFamily.Contains(".ttf")) fontFamily += ".ttf";
        var typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "fonts/" + fontFamily);

        Control.Typeface = typeface;
    }

}


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>