如何在底部的文本字段中设置单行?

时间:2015-09-24 04:55:49

标签: ios objective-c uitextfield

这是我在文本字段类别中使用的代码: 该行仅在用户触摸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;
         }

但我没有,请任何人帮我找出解决方案。

3 个答案:

答案 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 Bh​​umika&#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)

首先将UITextField放在UIViewController中。然后选择放置UITextField并转到Xcode右侧的属性,从那里你会发现Control你可以看到对齐选项,因为还有两个选项 - &gt;水平和垂直。在垂直方向,您可以看到在屏幕截图中可以看到的选择一个。通过单击该选项,您的文本数据将显示在UITextField底部。

In this image you can see Control->Alignment->Vetical->third option