Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add lefthand margin to UITextField

I want to put the left margin of a UITextField's text at 10 px. What is the best way to do that?

like image 944
iOSPawan Avatar asked Apr 15 '11 09:04

iOSPawan


1 Answers

You can do it by extending UITextField class and overriding two methods:

- (CGRect)textRectForBounds:(CGRect)bounds; - (CGRect)editingRectForBounds:(CGRect)bounds; 

Here is the code:

The interface in MYTextField.h

@interface MYTextField : UITextField  @end 

Its implementation in MYTextField.m

@implementation MYTextField  static CGFloat leftMargin = 28;   - (CGRect)textRectForBounds:(CGRect)bounds {     bounds.origin.x += leftMargin;      return bounds; }   - (CGRect)editingRectForBounds:(CGRect)bounds {     bounds.origin.x += leftMargin;      return bounds; } @end 
like image 62
Riad Krim Avatar answered Sep 25 '22 04:09

Riad Krim