自带的UITextFiled是不能设置内填充的,有时候我们需要留出一点空白出来显示图片或者别的,就需要设置填充。
我们可以继承UITextFiled类,重写 textRectForBounds: 和 ditingRectForBounds:
- (void)setPaddingLeft:(float)left right:(float)right top:(float)top bottom:(float)bottom {
_isPaddingEnable = YES;
_paddingLeft = left;
_paddingRight = right;
_paddingTop = top;
_paddingBottom = bottom;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
if (_isPaddingEnable) {
return CGRectMake(bounds.origin.x + _paddingLeft, bounds.origin.y + _paddingTop, bounds.size.width - _paddingRight, bounds.size.height - _paddingBottom);
} else {
return bounds;
}
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}