可自定义简单、好看的TextField

闲来无事,仿写了一个文本输入框,不知道怎么的录的这个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 我要做最骚的程序猿👨‍💻‍

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,856评论 25 709
  • 放暑假回家的时候,被苦大仇深大姑抓去当苦力帮她干活。她苦大仇深因为那些活本不该由她来干,而且作为一名即将退休的小镇...
    芦苇荡阅读 3,968评论 0 2
  • 火车上的生活,就是在煎熬中享受! 提起坐火车,想必大家都不会太陌生,每个人都坐过火车,无论是长途还是短途,火车总会...
    杨先生i阅读 4,314评论 12 9
  • 一入深山寻高师,曲幽通径百花饶。 问师漫生怎空来,师只掌立又指前。 一池岩石刻洗心,一屋一座刻洗心 取瓢清水砌杯茶...
    半睡半醒_84b7阅读 1,475评论 0 0
  • 2016最后一天一句话评价自己:感谢那个拼命的自己!虽然有时候都觉得熬不过去了,很高兴自己可以坚持下来。熬过了所有...
    27046a9c4d94阅读 1,579评论 1 2