参考:http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin
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
- (CGRect)textRectForBounds:(CGRect)bounds {
int leftMargin = 10;
CGRect inset = CGRectMake(bounds.origin.x + leftMargin, bounds.origin.y, bounds.size.width - leftMargin, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
int leftMargin = 10;
CGRect inset = CGRectMake(bounds.origin.x + leftMargin, bounds.origin.y, bounds.size.width - leftMargin, bounds.size.height);
return inset;
}
@end