gpt4 book ai didi

ios - 使用外观向 uitextfield 添加填充

转载 作者:行者123 更新时间:2023-11-28 21:57:17 24 4
gpt4 key购买 nike

我不想为了获得文本填充而子类化 UITextField 类。

我想用外观代理来做。

我试过这个:

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

不幸的是,如果我将此代码放入我的应用程序委托(delegate)中,任何包含 uitextfield 的 View Controller 都无法加载 View

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

最佳答案

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"

关于ios - 使用外观向 uitextfield 添加填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26121598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com