Get control's dimensions in CustomRenderer (Xa

2020-08-01 06:55发布

问题:

I've overridden OnElementChanged method inside a custom EntryRenderer which looks like this:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);

    if (Control != null)
    {
        var iconLabel = new UILabel();
        iconLabel.Font = FontAwesome.Font(12);
        iconLabel.Text = " " + FontAwesome.FAPlay;
        iconLabel.Frame = new CGRect(x: 5, y: 0, width: 20, height: 20);
        Control.LeftView = iconLabel;
        Control.LeftViewMode = UITextFieldViewMode.Always;

        Control.BorderStyle = UITextBorderStyle.None;
        var bottomLine = new CALayer();
        bottomLine.Frame = new CGRect(0.0, Control.Frame.Height - 1, Control.Frame.Width, 1.0);
        bottomLine.BackgroundColor = UIColor.White.CGColor;
        Control.Layer.AddSublayer(bottomLine);
    }
}

All I want to do is customize Entry control to add FontAwesome icon on the left and add a layer at the bottom to make it look like it only has bottom-border.

The problem is that Control.Frame doesn't have Width & Height (their value is 0).

Any help or another way to hack border bottom Entry (UITextField) style? Thanks in advance.

回答1:

All I want to do is customize Entry control to add FontAwesome icon on the left and add a layer at the bottom to make it look like it only has bottom-border.

Use a colored UIView added as Subview of the control and the clipping will be handled for you.

:-)

if (Control != null) {
    Control.BackgroundColor = UIColor.LightGray;
    var iconLabel = new UILabel();
    iconLabel.Font = FontAwesome.Font(12);
    iconLabel.Text = " " + FontAwesome.FAAmbulance;
    iconLabel.Frame = new CGRect(x: 5, y: 0, width: 20, height: 20);
    Control.LeftView = iconLabel;
    Control.LeftViewMode = UITextFieldViewMode.Always;

    Control.BorderStyle = UITextBorderStyle.None;
    Console.WriteLine ("cs: " + customSize);
    UIView myBox = new UIView (new CGRect (0.0, 20.0, 1000.0, 1.0)); 
    myBox.BackgroundColor = UIColor.Red;
    Control.AddSubview(myBox);
}

if (Control != null) {
    var iconLabel = new UILabel();
    iconLabel.Font = FontAwesome.Font(12);
    iconLabel.Text = " " + FontAwesome.FAPlay;
    iconLabel.Frame = new CGRect(x: 5, y: 0, width: 20, height: 20);
    Control.LeftView = iconLabel;
    Control.LeftViewMode = UITextFieldViewMode.Always;

    Control.BorderStyle = UITextBorderStyle.None;
    Console.WriteLine ("cs: " + customSize);
    UIView myBox = new UIView (new CGRect (0.0, 20.0, 1000.0, 1.0)); 
    myBox.BackgroundColor = UIColor.Black;
    Control.AddSubview(myBox);
}