Xamarin.Forms Label Renderer for Displaying many lines

I am back again with a post on Xamarin.Forms Custom Renderer. My earlier post was on the creation of a Custom Renderer for Entry.

Well, this time, we are going to solve something different which might not have been faced by many users. Sometimes, it happens that you want to display some lines of Text to the user like Read Me or just some Random text. For instance, consider the following code snippet:

<Label Text="{Binding SomeText}" HorizontalOptions="Fill">

Here, when the App is run, the text will be filled in the Label. However, if the number of lines crosses 100, the text will get clipped. In case, you want some more lines, you can always create a Custom Renderer for Label.

You can try to create something like this:

In Android

public class CustomLabelRenderer: LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        Control.SetMaxLines(500);
    }
}

In iOS

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

        if (Control != null)
        {
            UILabel label = Control;
            label.Lines = 500;
        }
    }
}

Hope this helps someone.

Happy Coding!
 

Anubhav Ranjan

Anubhav Ranjan

MVP - Windows Platform Development at Microsoft

Anubhav is a Program Freak who loves coding and currently works as a Consultant at Xamarin. He is very much passionate about Coding and solving complex problems. Believes in the motto "Eat, Drink...Code" from Windows AppFest 2012. He loves to work and develop on Microsoft technologies including Web Apps, Windows Phone Apps and Windows Store Apps.

He is an active member of the BDotnet(Bangalore .NET) User Group and Speaker/Member of XHackers - A Community for Cross-Platform App Development.
Anubhav Ranjan

Latest posts by Anubhav Ranjan (see all)

Leave a Reply