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

Proper way to set Label TextColor from Custom Renderer

$
0
0

Hi,

I have a custom class named SubtleLabel which has custom label renderers for each platform.

Custom Control

public class SubtleLabel : Label
{
}

iOS renderer

public class SubtleLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        this.Control.TextColor = Constants.SubtleColor;
    }
}

This was working before. I am on 1.2.2.6240-pre2 release.

I noticed that internally the code is calling UpdateText() from the OnElementChanged event

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        ...
        this.UpdateText();
        ...
    }

Where UpdateText is resetting the text color in UpdateColor().

As a workaround, I am setting the Element.TextColor in the iOS renderer, but I believe this will result in unnecessary conversions from Color to UIColor for each control. As Constants.SubtleColor is a cached static property.

Thanks


Viewing all articles
Browse latest Browse all 58056

Trending Articles



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