如图:
左边的icon太过于贴近左边
text输入也太贴近左边
而且UITextfield也没有提供相应的方法
不然就要让UI给切一个带有边距的图,或者添加一层父视图,其实并不需要
解决办法:
创建一个继承与UITextfield的类,重写以下方法
//leftView添加左边距
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect textRect = [super leftViewRectForBounds:bounds];
textRect.origin.x += kSuitLength(10);
return textRect;
}
//text位置添加左边距
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rect = [super textRectForBounds:bounds];
int margin = kSuitLength(9);
CGRect inset = CGRectMake(rect.origin.x + margin, rect.origin.y, rect.size.width - margin, rect.size.height);
return inset;
}
//编辑位置添加左边距
- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect rect = [super editingRectForBounds:bounds];
int margin = kSuitLength(9);
CGRect inset = CGRectMake(rect.origin.x + margin, rect.origin.y, rect.size.width - margin, rect.size.height);
return inset;
}
当然右边距也是一样~
效果图,这样看上去就顺眼很多了!
如有帮助,请点一下喜欢❤️❤️❤️