2
votes

I have used custom renderer for setting up a font.. The issue is when I change the text for the label the font style disappers..

Provided is my custom renderer code.. Can anyone tell me what I'm doing wrong??

[assembly: ExportRenderer (typeof (MyLabel), typeof (MyLabelRenderer))]
namespace FormsProj.Droid
{
public class MyLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged (ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged (e);

        var label = (TextView)Control;
        Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "Raleway-Regular.ttf");
        label.Typeface = font;
    }
}
}

Update #1 This is my Code Shared Project using System; using Xamarin.Forms;

namespace FormsProj
{
public class MyLabel:Label
 {
 }
}

Code Where I'm using MyLabel

var Thankyoutxt = new MyLabel {
            XAlign = TextAlignment.Center,
            Text = "Thank You",
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.Center,
            TextColor = Xamarin.Forms.Color.White,
            FontAttributes = FontAttributes.Bold,
            Style = Device.Styles.CaptionStyle,
            FontFamily = ColorandFont.FontName,

        };

On a click of the button I will change text, By that time the font changes back without custom font..

Thanks in advance.. :)

1
Where are you changing the text? I'm only seeing the code for changing the font.John Odom
@JohnOdom Chaging text code is like label.Text ="Edited"; I assumed problem lies within Renderer..Femil Shajin
I was hoping that you would post the code on where you are changing the text. I already know how to change the text :P. It could maybe be an issue with how you are calling the renderer.John Odom
@JohnOdom Sorry :) I was just explaining that I have done nothing more than that one line..Femil Shajin
Well how about the `MyLabelRenderer'? Can you show how you are using that?John Odom

1 Answers

0
votes

You may want to try resetting the properties on the OnPropertyChangedEvent in the renderer as well. I have had issues with this in the past, and this has fixed it.