调整TextField占位文字颜色

2017-03-09  本文已影响72人  ShenYj

需求:
1.通过不同状态调整TextField占位文字颜色
2.编辑状态下为WhiteColor;默认LightGrayColor
文字描述较为抽象,直接看效果图:

01-占位文字颜色效果.gif
实现方式一

实现思路:
设置TextField代理,实现<UITextFieldDelegate>协议方法,在开始编辑时,通过富文本直接对TextFieldattributedPlaceholder赋值,同理,在编辑结束后同样操作

当然,在为TextField设置占位文字时,也需要通过富文本对attributedPlaceholder属性进行赋值

- (void)textFieldDidBeginEditing:(JSLoginTextField *)textField {
    NSMutableAttributedString *placeholderText = [[NSMutableAttributedString alloc] initWithAttributedString:textField.attributedPlaceholder];
    [placeholderText addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, placeholderText.length)];
    textField.attributedPlaceholder = placeholderText;
}
- (void)textFieldDidEndEditing:(JSLoginTextField *)textField {
    NSMutableAttributedString *placeholderText = [[NSMutableAttributedString alloc] initWithAttributedString:textField.attributedPlaceholder];
    [placeholderText addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, placeholderText.length)];
    textField.attributedPlaceholder = placeholderText;
}

实现方式二

实现思路:
实现代理的方式有点大材小用了,其实还可以addTarget的方式
分别在UIControlEventEditingDidBeginUIControlEventEditingDidEnd中做上面的操作


实现方式三

实现思路:
通知,在自定义TextField中,分别监听UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidEndEditingNotification,在接收到对应通知后,在方法中执行上面的操作

[[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(beingEditing:) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextFieldTextDidEndEditingNotification object:self];
注意点:

实现方式四

实现思路:
1.自定义TextField;
2.通过RunTime获取到私有属性placeholderLabel占位文本框;
3.重写layoutSubviews方法,根据当前TextField状态通过KVC设置占位文本框字体颜色

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.isEditing) {
        [self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
    } else {
        // KVC 修改 占位文字颜色
        [self setValue:[UIColor lightGrayColor] forKeyPath:@"placeholderLabel.textColor"];
    }
}

实现方式五

实现思路:
重写becomeFirstResponderresignFirstResponder方法,分别对当前TextField处理

- (void)layoutSubviews {
    [super layoutSubviews];
    if (!self.isEditing) {
        [self setValue:[UIColor lightGrayColor] forKeyPath:@"placeholderLabel.textColor"];
    }
}
- (BOOL)becomeFirstResponder {
    [self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder {
    [self setValue:[UIColor lightGrayColor] forKeyPath:@"placeholderLabel.textColor"];
    return [super resignFirstResponder];
}

但需要注意一点,因为TextField在实例化时,默认并未设置placeholder属性,TextField内部子控件placeholderLabel采用一种懒加载机制,此时相当于为nil,如果在自定义TextFieldinit方法中直接通过KVC方式设置默认状态颜色是无意的,必须要保证在placeholderLabel存在的前提下设置才会有意义,而设置延迟的方式并不能完美解决这个问题,文字描述可能比较抽空,直接看图便能更容易的发现这个问题:

02-占位文字颜色效果.gif

虽然beignEditendEdit下的状态正常,但是刚刚展示视图时,TextField的占位文字还是黑色,暂时想到的方法就是在layoutSubViews方法中(方式四中),将未编辑状态下设置成灰色


总结:

当然通过协议/addTarget的方式,也可以封装到TextField内部
① 将代理设置给自己,实现协议方法,但这种将代理设置给自己的方式不推荐使用,因为代理模式为一对一,我们将TextField的代理对象设置给了自己,而外界使用TextField并设置代理后,问题就来了
② 而addTarget的方式不受影响,在TextField内部添加自己监听状态,在外界添加控制器监听TextField状态,当条件满足时,TextField控制器都会分别响应自身内部的事件,不受影响

通知的特点是方法执行所在线程取决于监听通知的位置,比如在主线程监听通知,那么收到通知后,方法将在主线程执行,反之则在子线程,而大部分的系统方法均在主线程执行,除了最常使用的addObserver:,还可以用另外一种指定线程的方式:

self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidBeginEditingNotification object:self queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification * _Nonnull note) {
        // 执行代码
    }];

上一篇下一篇

猜你喜欢

热点阅读