简体   繁体   中英

Xamarin: iOS 7 UITextField Padding

What's the "easiest" way to add padding (all four sides) to a UITextField in Xamarin for iOS 7. Following code doesn't seem to work on iOS7.

textField.LeftView = new UIView (new RectangleF (0, 0, 10, 10));
textField.LeftViewMode = UITextFieldViewMode.Always;

Then I tried to subclass UITextField to try another option...

public class TextFieldBase : UITextField
{
    public TextFieldBase (IntPtr handle) : base (handle)
    {
    }

    public override RectangleF TextRect(RectangleF bounds){
        return RectangleF.Inflate( bounds , 10 , 10 );
    }

    public override RectangleF EditingRect(RectangleF bounds){
        return RectangleF.Inflate( bounds , 10 , 10 );
    }
}

That didn't work either.

I'm not sure if what I have set in the interface builder is overriding what I have in the subclass code.

Subclass UITextField and override TextRect, EditingRect:

public override CoreGraphics.CGRect EditingRect (CoreGraphics.CGRect forBounds) {
    return base.EditingRect (InsetRect (forBounds, new UIEdgeInsets (0, 10, 0, 10)));
}

public static CoreGraphics.CGRect InsetRect(CoreGraphics.CGRect rect, UIEdgeInsets insets)
{
    return new CoreGraphics.CGRect(rect.X + insets.Left, rect.Y + insets.Top,
        rect.Width - insets.Left - insets.Right, rect.Height - insets.Top - insets.Bottom );
}

You might want to try subclassing UITextField and overriding the following methods: - (CGRect)textRectForBounds and -(CGRect)editingRectForBounds.

You can then adjust the bounds, height, and width to get the desired effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM