Maui custom iOS picker renderer conversion

51 views Asked by At

I have a xamarin custom picker renderer that has the drop down arrow I am trying to convert to a handler in MAUI. I have successfully converted the Android one but I am at a loss for iOS. Any tips here?

xamarin renderer:

public class CustomPickerRenderer: PickerRenderer {
  public CustomPicker ElementV2 => Element as CustomPicker;
  public UITextFieldPadding ControlV2 => Control as UITextFieldPadding;
  private static string _defaultDownArrow = "\uf0d7";

  protected override UITextField CreateNativeControl() {
    var control = new UITextFieldPadding(RectangleF.Empty) {
      Padding = ElementV2.Padding,
      BorderStyle = UITextBorderStyle.RoundedRect,
      ClipsToBounds = true
    };

    UpdateBackground(control);

    return control;
  }

  protected void UpdateBackground(UITextField control) {
    if (control == null) return;
    control.Layer.CornerRadius = ElementV2.CornerRadius;
    control.Layer.BorderWidth = ElementV2.BorderThickness;
    control.Layer.BorderColor = ElementV2.BorderColor.ToCGColor();

    var arrowTextSymbol = !string.IsNullOrWhiteSpace(ElementV2.IconGlyph) ? ElementV2.IconGlyph: _defaultDownArrow;
    var downArrow = new UILabel {
      Text = $ "{arrowTextSymbol}   ",
      TextColor = ElementV2.IconColor.ToUIColor(),
      Font = UIFont.FromName(ElementV2.IconFontFamily, (float) ElementV2.IconSize),
      TextAlignment = UITextAlignment.Center
    };

    control.RightView = downArrow;
    control.RightViewMode = UITextFieldViewMode.Always;
  }

  protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
    if (e.PropertyName == CustomPicker.PaddingProperty.PropertyName) {
      UpdatePadding();
    }

    if (e.PropertyName == CustomPicker.BorderColorProperty.PropertyName) {
      UpdateBorderColor();
    }

    if (e.PropertyName == CustomPicker.IconGlyphProperty.PropertyName) {
      UpdateIcon();
    }

    base.OnElementPropertyChanged(sender, e);
  }

  protected void UpdateBorderColor() {
    if (Control == null) return;

    ControlV2.Layer.BorderColor = ElementV2.BorderColor.ToCGColor();
  }

  protected void UpdatePadding() {
    if (Control == null) return;

    ControlV2.Padding = ElementV2.Padding;
  }

  protected void UpdateIcon() {
    if (Control == null) return;

    var arrowTextSymbol = !string.IsNullOrWhiteSpace(ElementV2.IconGlyph) ? ElementV2.IconGlyph: _defaultDownArrow;
    var downArrow = new UILabel {
      Text = $ "{arrowTextSymbol}   ",
      TextColor = ElementV2.IconColor.ToUIColor(),
      Font = UIFont.FromName(ElementV2.IconFontFamily, (float) ElementV2.IconSize)
    };

    ControlV2.RightView = downArrow;
    ControlV2.RightViewMode = UITextFieldViewMode.Always;

    ControlV2.LeftView = new UIView(new CGRect(0, 0, 10, 0));
    ControlV2.LeftViewMode = UITextFieldViewMode.Always;
    ControlV2.Padding = ElementV2.Padding;
  }
}

Text field with padding:

public class UITextFieldPadding: UITextField {
  private Thickness _padding = new Thickness(5);

  public Thickness Padding {
    get =>_padding;
    set {
      if (_padding != value) {
        _padding = value;
        //InvalidateIntrinsicContentSize();
      }
    }
  }

  public UITextFieldPadding() {}
  public UITextFieldPadding(NSCoder coder) : base(coder) {}

  public UITextFieldPadding(CGRect rect) : base(rect) {}

  public override CGRect TextRect(CGRect forBounds) {
    var insets = new UIEdgeInsets((float) Padding.Top, (float) Padding.Left, (float) Padding.Bottom, (float) Padding.Right);
    return insets.InsetRect(forBounds);
  }

  public override CGRect PlaceholderRect(CGRect forBounds) {
    var insets = new UIEdgeInsets((float) Padding.Top, (float) Padding.Left, (float) Padding.Bottom, (float) Padding.Right);
    return insets.InsetRect(forBounds);
  }

  public override CGRect EditingRect(CGRect forBounds) {
    var insets = new UIEdgeInsets((float) Padding.Top, (float) Padding.Left, (float) Padding.Bottom, (float) Padding.Right);
    return insets.InsetRect(forBounds);
  }
}
0

There are 0 answers