Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text inset for UITextField?

I would like to inset the text of a UITextField.

Is this possible?

like image 425
RunLoop Avatar asked Apr 22 '10 20:04

RunLoop


People also ask

How do I add padding to textField?

A good approach to add padding to UITextField is to subclass and add an edgeInsets property. You then set the edgeInsets and the UITextField will be drawn accordingly. This will also function correctly with a custom leftView or rightView set.

What is UITextField?

An object that displays an editable text area in your interface.


2 Answers

Overriding -textRectForBounds: will only change the inset of the placeholder text. To change the inset of the editable text, you need to also override -editingRectForBounds:

// placeholder position - (CGRect)textRectForBounds:(CGRect)bounds {      return CGRectInset(bounds, 10, 10); }  // text position - (CGRect)editingRectForBounds:(CGRect)bounds {      return CGRectInset(bounds, 10, 10); } 
like image 60
dvs Avatar answered Oct 08 '22 05:10

dvs


I was able to do it through:

myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0); 

Of course remember to import QuartzCore and also add the Framework to your project.

like image 20
chuthan20 Avatar answered Oct 08 '22 05:10

chuthan20