这是我在文本字段类别中使用的代码: 该行仅在用户触摸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;
}
但我没有,请任何人帮我找出解决方案。
答案 0 :(得分:2)
您需要创建一个自定义TextField类,并在该类中设置委托。喜欢这个
.h FIle
@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];
可能会帮助你。
答案 1 :(得分:1)
您可以根据UITextField
的 Bhumika&#39> 答案创建自定义类,也可以使用以下类别。
我已创建了类别UITextField+Extended
@interface UITextField (Extended)
-(void)addBottomLine;
-(void)removeBottomLine;
@end
.m 文件中的
@implementation UITextField (Extended)
-(void)addBottomLine
{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.name = @"bottomLine";
border.borderColor = [UIColor redColor].CGColor;//[UIColor colorFromHexString:@"#ffc400"].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, 4);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
}
-(void)removeBottomLine
{
[[self.layer.sublayers copy] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CALayer * subLayer = obj;
if(([[subLayer name] isEqualToString:@"bottomLine"])){
[subLayer removeFromSuperlayer];
}
}];
}
@end
和在您的视图控制器中简单导入类并调用此方法以使用委托添加/删除图层。
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtTemp resignFirstResponder];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[txtTemp addBottomLine];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[txtTemp removeBottomLine];
}
希望这会对你有所帮助。
答案 2 :(得分:0)