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

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

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

 

1.放置UILabel

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

2.修改textField.attributedPlaceholder属性

//通过NSAttributedString来更改placeholder颜色。此种方法比较麻烦的是,需要知道页面上所有的textField什么时候聚焦,什么时候失焦(有两种方法:A.监听键盘弹出的通知  B.通过UITextField的代理方法textFieldDidBeginEditing),然后再判断哪个是白色,哪个是灰色。如果页面上textField非常多,就需要写很多的if语句。

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

    self.phoneTextField.attributedPlaceholder = attrString;

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

   具体实现见代码:

//该方法的好处是不用获取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设值。

//  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

 

 

 

 

转载于:https://www.cnblogs.com/stevenwuzheng/p/5483332.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值