You should override textRectForBounds
and editingRectForBounds
like this:
CustomTextField.h:
#import <UIKit/UIKit.h>
@interface CustomTextField : UITextField
@property CGFloat inset;
@end
CustomTextField.m:
#import "CustomTextField.h"
@implementation CustomTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rect = CGRectInset(bounds, _inset, 0);
return rect;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect rect = CGRectInset(bounds, _inset, 0);
return rect;
}
@end
ViewController.m:
#import "ViewController.h"
#import "CustomTextField.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet CustomTextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.inset = 20;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
The result:
