在 UITextField 下放置一行的正确方法是什么
What's the correct way to put a line under a UITextField
我想要一个无边框的 UITextField,但下面有一个 1 或 2 像素高的引导线,用于强调该字段。需要明确的是,我不希望 UITextField 中的文本被加下划线,而是该字段,无论它是空的还是充满文本。
这个例子见GoogleMaterial设计:
所以我当然可以坚持使用显示纯色的 UIImage 并使其适合 space,但这看起来不太优雅,有没有更好的方法 [=21] =] 和未来证明,如果我担心我的代码将如何工作几代 iOS 以及谁知道什么设备?
p.s。宁愿使用 Obj-C 从头开始,也不愿找一个库。
实际上我昨天刚刚做了这个...子类 UITextField,这会起作用。
注意:使用 textFieldDidBeginEditing: 和 textFieldDidEndEditing: 通过 NSNotificationCenter 或委托方法更改行高和颜色会更清晰。
- (void)setup
{
self.hairlineLayer = [CALayer layer];
self.hairlineLayer.backgroundColor = [UIColor materialTextDarkDividerColor].CGColor;
[self.layer addSublayer:self.hairlineLayer];
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat hairlineHeight = 0;
if (self.isFirstResponder) {
self.hairlineLayer.backgroundColor = [UIColor redColor].CGColor;
hairlineHeight = 1.4;
} else {
self.hairlineLayer.backgroundColor = [UIColor grayColor].CGColor;
hairlineHeight = 1/[UIScreen mainScreen].scale;
}
self.hairlineLayer.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - hairlineHeight, CGRectGetWidth(self.bounds), hairlineHeight);
}
创建一个新的 GlobalClass
在GlobalClass.h
中声明一个方法
+(void)UnderLineStyleTextField:(UITextField *)textField;
在 GlobalClass.m
中定义一个方法
+(void)UnderLineStyleTextField:(UITextField *)textField
{
CALayer *textFieldbottomBorder = [CALayer layer];
textFieldbottomBorder.frame = CGRectMake(0.0f, textField.frame.size.height - 1, textField.frame.size.width, 1.0f);
textFieldbottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[textField.layer addSublayer:textFieldbottomBorder];
}
在 ViewController
中导入 GlobalClass
从 ViewDidLoad 调用 GlobalClass 方法
[GlobalClass UnderLineStyleTextField:txtForgotPasswods];
我想要一个无边框的 UITextField,但下面有一个 1 或 2 像素高的引导线,用于强调该字段。需要明确的是,我不希望 UITextField 中的文本被加下划线,而是该字段,无论它是空的还是充满文本。
这个例子见GoogleMaterial设计:
所以我当然可以坚持使用显示纯色的 UIImage 并使其适合 space,但这看起来不太优雅,有没有更好的方法 [=21] =] 和未来证明,如果我担心我的代码将如何工作几代 iOS 以及谁知道什么设备?
p.s。宁愿使用 Obj-C 从头开始,也不愿找一个库。
实际上我昨天刚刚做了这个...子类 UITextField,这会起作用。
注意:使用 textFieldDidBeginEditing: 和 textFieldDidEndEditing: 通过 NSNotificationCenter 或委托方法更改行高和颜色会更清晰。
- (void)setup
{
self.hairlineLayer = [CALayer layer];
self.hairlineLayer.backgroundColor = [UIColor materialTextDarkDividerColor].CGColor;
[self.layer addSublayer:self.hairlineLayer];
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat hairlineHeight = 0;
if (self.isFirstResponder) {
self.hairlineLayer.backgroundColor = [UIColor redColor].CGColor;
hairlineHeight = 1.4;
} else {
self.hairlineLayer.backgroundColor = [UIColor grayColor].CGColor;
hairlineHeight = 1/[UIScreen mainScreen].scale;
}
self.hairlineLayer.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - hairlineHeight, CGRectGetWidth(self.bounds), hairlineHeight);
}
创建一个新的 GlobalClass
在GlobalClass.h
中声明一个方法+(void)UnderLineStyleTextField:(UITextField *)textField;
在 GlobalClass.m
中定义一个方法+(void)UnderLineStyleTextField:(UITextField *)textField
{
CALayer *textFieldbottomBorder = [CALayer layer];
textFieldbottomBorder.frame = CGRectMake(0.0f, textField.frame.size.height - 1, textField.frame.size.width, 1.0f);
textFieldbottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[textField.layer addSublayer:textFieldbottomBorder];
}
在 ViewController
中导入 GlobalClass从 ViewDidLoad 调用 GlobalClass 方法
[GlobalClass UnderLineStyleTextField:txtForgotPasswods];