Help with label renderer

MJ_AhmedMJ_Ahmed BHMember ✭✭

I am trying to display the html in my label control, so far I am not able to display the label as well as html text as yet. My renderers are as follows:

My label (defined as a class) is:

public class MyHTMLlabel : Label { }

for Android:

[assembly: ExportRenderer(typeof(MyHTMLlabel), typeof(HTMLrenderer))]
namespace App1.Droid
{
class HTMLrenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs

        Control?.SetText(Html.FromHtml(Element.Text), TextView.BufferType.Spannable);
    }

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

        if (e.PropertyName == Label.TextProperty.PropertyName)
        {
            Control?.SetText(Html.FromHtml(Element.Text), TextView.BufferType.Spannable);
        }
    }
}

}

for iOS:

[assembly: ExportRenderer(typeof(MyHTMLlabel), typeof(HTMLrenderer))]
namespace App1.iOS
{
class HTMLrenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs

        if (Control != null && Element != null && !string.IsNullOrWhiteSpace(Element.Text))
        {
            var attr = new NSAttributedStringDocumentAttributes();
            var nsError = new NSError();
            attr.DocumentType = NSDocumentType.HTML;

            var myHtmlData = NSData.FromString(Element.Text, NSStringEncoding.Unicode);
            Control.Lines = 0;
            Control.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
        }
    }

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

        if (e.PropertyName == Label.TextProperty.PropertyName)
        {
            if (Control != null && Element != null && !string.IsNullOrWhiteSpace(Element.Text))
            {
                var attr = new NSAttributedStringDocumentAttributes();
                var nsError = new NSError();
                attr.DocumentType = NSDocumentType.HTML;

                var myHtmlData = NSData.FromString(Element.Text, NSStringEncoding.Unicode);
                Control.Lines = 0;
                Control.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
            }
        }
    }
}

}

calling it in my page:

var myLabel = new MyHTMLlabel { Text = body }; //where the "body" hold the text with html tags

I will really appreciate for a help as I'm stuck with the issue :) Where am I doing wrong?

Sign In or Register to comment.