I have forms app, and I have custom label as following,
public class HtmlFormattedLabel : Label
{
public float mySize { get; set; }
}
and I have custom renderer in both android and ios as following:
Android:
[assembly: ExportRenderer(typeof(HtmlFormattedLabel), typeof(HtmlFormattedLabelRenderer))]
namespace HBRS.Droid.Renderers
{
public class HtmlFormattedLabelRenderer : LabelRenderer
{
public HtmlFormattedLabelRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
HtmlFormattedLabel view = (HtmlFormattedLabel)Element;
if (view == null) return;
Control.Gravity = Android.Views.GravityFlags.Right;
Control.SetTextSize(Android.Util.ComplexUnitType.Sp, view.mySize);
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.N)
Control.SetText(Html.FromHtml(view.Text.ToString(), FromHtmlOptions.ModeLegacy), TextView.BufferType.Normal);
else
Control.SetText(Html.FromHtml(view.Text.ToString()), TextView.BufferType.Normal);
}
}
}
iOS
[assembly: ExportRenderer(typeof(HtmlFormattedLabel), typeof(HtmlFormattedLabelRenderer))]
namespace HBRS.iOS.Renderers
{
public class HtmlFormattedLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var view = (HtmlFormattedLabel)Element;
if (view == null) return;
UIStringAttributes te = new UIStringAttributes();
var attr = new NSAttributedStringDocumentAttributes();
var nsError = new NSError();
attr.DocumentType = NSDocumentType.HTML;
attr.StringEncoding = NSStringEncoding.UTF8;
attr.ViewMode = NSDocumentViewMode.PageLayout;
Control.AttributedText = new NSAttributedString(view.Text, attr, ref nsError);
}
}
}
the problem that in android, the renderer applied to all labels in the app, (when I change size of the custom label all the label sizes changes)