0

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.

Karthik Murugesan
  • 1,100
  • 3
  • 13
  • 25

2 Answers2

1

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 );
}
Frans Hanyane
  • 237
  • 2
  • 12
0

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.

pnavk
  • 4,552
  • 2
  • 19
  • 43
  • that'a exactly what I'm doing in the second part of the question. I think this is not working because somehow what I have in the interface builder is overriding? – Karthik Murugesan Mar 12 '14 at 18:47
  • hmm, how about trying this sublayerTransform property in the layer. See here http://stackoverflow.com/a/13515749/2754727 – pnavk Mar 12 '14 at 20:28