使用外观向uitextfield添加填充



我不想仅仅为了获得文本填充而对UITextField类进行子类化

我想使用外观代理来完成此操作。

我试过这个:

UIView *padding = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 2)];
[[UITextField appearance] setLeftView:padding];
[[UITextField appearance] setLeftViewMode:UITextFieldViewModeAlways];

不幸的是,如果我把这段代码放在我的应用程序委托中,任何包含uitextfield的视图控制器都无法加载视图

如何在不进行子类化的情况下向文本字段添加填充?

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;
}

(或)

喜欢这个吗

// 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"

最新更新