gpt4 book ai didi

ios - 如何在底部的文本字段内设置单行?

转载 作者:行者123 更新时间:2023-11-28 19:44:47 24 4
gpt4 key购买 nike

这是我在文本字段类别中使用的代码:该行仅在用户触摸 textField 时显示。

 @implementation UITextField (textField)

-(void)textBottomLine:(UITextField *)textField
{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor colorFromHexString:@"#ffc400"].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, 4);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
}

但我一无所获,请任何人帮助我找出解决方案。

最佳答案

您需要创建一个自定义 TextField 类并在该类中设置委托(delegate)。像这样

.h 文件

@interface CustomTextField : UITextField<UITextFieldDelegate>
@propery(strong)CALayer *border;
@end

.m 文件

@implementation CustomTextField
@synthesis border;


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.delegate = self;
}
return self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor colorFromHexString:@"#ffc400"].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, 4);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[border removeFromSuperlayer];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
@end

在任何需要文本字段的地方都可以使用:

CustomTexTField *textField = [[CustomTextField alloc] initWithFrame:customFrame];
[self.view addSubView:textField];

也许它会对你有所帮助。

关于ios - 如何在底部的文本字段内设置单行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32753638/

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