[BS-19]更改UITextField的placeholder文字颜色的5种方法

更改UITextField的placeholder文字颜色的5种方法

 

想要达到的目标是:一个页面上有多个UITextField,当用户聚焦某textField时,该文本框的placeholder的文字会灰色变为白色,当文本框失去焦点时,placeholder颜色从白色再变回灰色。

 

1.放置UILabel

最简单最笨的方法是在每一个textField里放一个UILabel来充当placeholder,当该textField聚焦时,让placeholder的文字会灰色变为白色,失焦后从白色再变回灰色。这种方法须要对每一个UILabel和TextField拖线,经过监听键盘通知或者UITextField的代理来获悉textField是否聚焦,而后写一堆if语句来判断。spa

2.修改textField.attributedPlaceholder属性

//经过NSAttributedString来更改placeholder颜色。此种方法比较麻烦的是,须要知道页面上全部的textField何时聚焦,何时失焦(有两种方法:A.监听键盘弹出的通知  B.经过UITextField的代理方法textFieldDidBeginEditing),而后再判断哪一个是白色,哪一个是灰色。若是页面上textField很是多,就须要写不少的if语句。代理

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];code

    self.phoneTextField.attributedPlaceholder = attrString;blog

3. 利用UITextField的子类重写drawPlaceholderInRect:方法

   具体实现见代码:it

//该方法的好处是不用获取Xib中textField的引用,不用拖线,直接将Xib中textField的class改成自定义类便可。之后有任何textField想要改变placeholder的颜色,直接定义为该类便可。但仍是须要本身监控UITextField是否聚焦。

#import "WZCustomPlaceholderTextField.h"

@implementation WZCustomPlaceholderTextField
/**
 @interface NSString(NSStringDrawing)  NSString的分类
 - (CGSize)sizeWithAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
 - (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
 - (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
 @end
 */
- (void)drawPlaceholderInRect:(CGRect)rect {//rect表明该自定义textField的frame/rect
//由于placeholder属于NSString类型,全部的NSString都有drawInRect方法,但此方法彷佛只在draw开头方法中有效
    [self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, rect.size.height) withAttributes:@{
                                                                                                       NSForegroundColorAttributeName:[UIColor whiteColor],
                                                                                                       NSFontAttributeName:[UIFont systemFontOfSize:15]
                                                                                                       }];
    
}


- (void)drawRect:(CGRect)rect {//rect表明该自定义textField的frame/rect
    [super drawRect:rect]; //调用父类UITextField的drawRect方法,将自定义尺寸传进去。必须调用父类
  
}

@end

4.利用UITextField的子类重写drawRect:方法,在drawRect中经过[self.placeholder drawInRect:]来进行设置。

5.经过KVC向UITextField隐藏的内部实例变量_placeholderLabel设值。

利用runtime打印出UITextField全部的实例变量,发现有个叫_placeholderLabel的内部实例变量,在自定义类中经过KVC设值。此方法好处是:利用KVC设值方便简洁,并且能够写在任何位置。本例中咱们重写becomeFirstResponder:和resignFirstResponder方法来监控UITextField是否聚焦,而后在其中利用KVC设值。io

//  WZTextField.m

// 利用runtime打印出UITextField全部的实例变量,发现有个叫_placeholderLabel的内部实例变量,在自定义类中经过KVC设值。
//

#import "WZTextField.h"

@implementation WZTextField
//调用顺序1 (从xib建立时调用)
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
    }
    return self;
}
//调用顺序2 (从xib建立时调用)
- (void)awakeFromNib {

}

//调用顺序3 (不管xib仍是纯代码建立都会调用)
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    self.tintColor = self.textColor; //设置光标颜色和文字颜色同样
}

- (BOOL)becomeFirstResponder {
    [self setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    return [super becomeFirstResponder];
}

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

@end
相关文章
相关标签/搜索