Search code examples
iosformsxamarincustom-renderer

Custom renderer doesn't work in iOS + library


I don't know why my Xamarin.Forms custom renderer doesn't work if I put it into a library and only on iOS, somebody could help me?

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

            if (Control == null) return;
            UpdateMaxLines();
            UpdateText();
        }

It works fine on Android, UWP and iOS if into the project.

https://github.com/matteobortolazzo/HtmlLabelPlugin


Solution

  • Add a do-nothing Initialize static method to your HtmlLabelRenderer class to insure your renderer types are loaded before Forms

    Example:

    public static void Initialize()
    {
    }
    

    Usage:

    In your AppDelegate before Forms.Init(), call your Initialize method:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        Plugin.HtmlLabel.iOS.HtmlLabelRenderer.Initialize();
        global::Xamarin.Forms.Forms.Init();
    
        LoadApplication(new App());
    
        return base.FinishedLaunching(app, options);
    }