Search code examples
xamarin.formsxamarin.ioslabelfont-sizecustom-renderer

Unchangeable font size in LabelCustomRenderer


I've made a Custom Renderer for a Label to display html formatted text.

The problem is that unlike in Android, in IOS the text is to small and i can't find a way to change it.

My code

IOS

[assembly:ExportRenderer(typeof(HtmlFormattedLabel), typeof(HtmlFormattedLabelRenderer))]
namespace YmoApp.iOS
{
    public class HtmlFormattedLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            var view = (HtmlFormattedLabel)Element;

            if (view == null) return;

            var attr = new NSAttributedStringDocumentAttributes();
            var nsError = new NSError();
            attr.DocumentType = NSDocumentType.HTML;

            var myHtmlData = NSData.FromString(view.Text, NSStringEncoding.UTF8);

            Control.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);

        }
    }
}

Android

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

            var view = (HtmlFormattedLabel)Element;
            if (view == null) return;

            Control.SetText(Html.FromHtml(view.Text.ToString()), TextView.BufferType.Spannable);
        }
    }
}

PCL

var descriptionLabel = new HtmlFormattedLabel()
{
    Text = "<http><body style='font-size; 20px'><p style='text-align: justify; font-family: Arial, Helvetica, sans-serif; font-size: 14px'>"
                + _imovel.Anuncio.Descricao + "</p></body></http>",
    Margin = new Thickness(20, 0)
};

descriptionLabel.FontSize = 14;

Nothing seems to take effect.


Solution

  • The error is in <body style='font-size; 20px'>.

    The ; sould be replaced with :.