我不想仅仅为获取文本填充而继承UITextField类。
我想使用外观代理来完成它。
我试过了:
UIView *padding = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 2)];
[[UITextField appearance] setLeftView:padding];
[[UITextField appearance] setLeftViewMode:UITextFieldViewModeAlways];
不幸的是,如果我将此代码放在我的app委托中,任何包含uitextfield的视图控制器都无法加载视图
如何在不对其进行子类化的情况下向文本字段添加填充?
答案 0 :(得分:2)
Can you try these..
- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + bounds.size.width/2, bounds.origin.y, bounds.size.width/2 - margin, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + bounds.size.width/2, bounds.origin.y, bounds.size.width/2 - margin, bounds.size.height);
return inset;
}
(OR)
喜欢这个
// Do any additional setup after loading the view from its nib.
UIView *paddingTxtfieldView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 42)];// what ever you want
txtfield.leftView = paddingTxtfieldView;
txtfield.leftViewMode = UITextFieldViewModeAlways;
txtfield.returnKeyType = UIReturnKeyNext;// if you want to go next textfiled write this otherwise "UIReturnKeyDone"