iOS 控件封装UI进价iOS开发技术分享

可自定义简单、好看的TextField

2017-07-31  本文已影响431人  Renjiee

闲来无事,仿写了一个文本输入框,不知道怎么的录的这个GIF有色盲效果,怎么办我也很难受啊。

这是一个带有色盲效果的GIF.gif

正题👇

可自定义属性

/** 文本输入框 */
@property (nonatomic, strong) UITextField * textField;


/** 占位符文本 */
@property (nonatomic, copy) NSString * placeholder;
/** 图片名称 */
@property (nonatomic, copy) NSString * leftIconName;
/** 文本颜色(默认DarkGray) */
@property (nonatomic,strong) UIColor *textColor;
/** 错误提示信息 */
@property (nonatomic, copy) NSString * errorStr;
/** 错误提示信息字体颜色 */
@property (nonatomic, strong) UIColor * errorLableColor;
/** 最大字数(必填) */
@property (nonatomic,assign) NSInteger maxLength;
/** 字数限制文本颜色(默认灰色) */
@property (nonatomic,strong) UIColor *textLengthLabelColor;
/** 底部分割线默认颜色 */
@property (nonatomic, strong) UIColor * lineDefaultColor;
/** 底部线条错误警告颜色(默认红色) */
@property (nonatomic,strong) UIColor *lineWarningColor;
/** 底部线条选中颜色(默认深绿色) */
@property (nonatomic,strong) UIColor *lineSelectedColor;

UITextFieldDelegate

#pragma mark -------------------- UITextFieldDelegate --------------------
- (void)textFieldEditingChanged:(UITextField *)sender
{
    if (sender.text.length > self.maxLength) {
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.errorLabel.alpha = 1.0;
            self.errorLabel.textColor = self.lineWarningColor;
            self.bottomLine.backgroundColor = self.lineWarningColor;
            self.lengthLabel.textColor = self.lineWarningColor;
            self.textField.textColor = self.lineWarningColor;
            //self.placeHolderLabel.textColor = self.lineWarningColor;
        } completion:nil];
    }else{
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.errorLabel.alpha = 0.0;
            self.bottomLine.backgroundColor = self.lineSelectedColor;
            self.lengthLabel.textColor = self.textLengthLabelColor;
            self.textField.textColor = self.textColor;
            //self.placeHolderLabel.textColor = self.placeHolderLabelColor;
        } completion:nil];
    }
    self.lengthLabel.text = [NSString stringWithFormat:@"%zd/%zd",sender.text.length,self.maxLength];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self setPlaceHolderLabelHidden:NO];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [self setPlaceHolderLabelHidden:YES];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self endEditing:YES];
    
    [self setPlaceHolderLabelHidden:YES];
    
    return YES;
}

占位符提示

#pragma mark -------------------- 占位符提示 --------------------
- (void)setPlaceHolderLabelHidden:(BOOL)isHidden
{
    if (isHidden) {
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.headerPlaceLabel.alpha = 0.0f;
            self.textField.placeholder = self.placeholder;
            self.bottomLine.backgroundColor = self.lineDefaultColor;
        } completion:nil];
    }else{
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.headerPlaceLabel.alpha = 1.0f;
            self.headerPlaceLabel.text = self.placeholder;
            self.textField.placeholder = @"";
            self.bottomLine.backgroundColor = self.lineSelectedColor;
        } completion:nil];
    }
}

这里是👉GitHub👈下载链接
有喜欢的童鞋记得给个Stare🙏🏻🙏🏻🙏🏻

我是Renjiee 我要做最骚的程序猿👨‍💻‍

上一篇下一篇

猜你喜欢

热点阅读