iOS-UITextField中给placeholder动态设置颜色的四种方法


  • 思路分析:
    • 0.自定义UITextField
    • 1.设置占位文字的颜色找-->placeholderColor,结果发现UITextField没有提供这个属性
    • 2.在storyboard/xib中寻找设置placeholder的属性,也没有发现
    • 3.发现UITextField中有- (void)drawPlaceholderInRect:(CGRect)rect;,这个方法用到QuartzD中的知识,可以试试;
    • 4.打印UITextField的子控件,查看是否有设置placeholder的属性
    • 5.用运行时,查看UITextField中内部的结构,看有没有隐藏属性,利用KVC,可以强制使用任何私有属性
      -

方法一:

  • 缺点:只能设置一次状态,不能动态的改变Placeholder的颜色
- (void)drawPlaceholderInRect:(CGRect)rect
{
    // 设置富文本属性
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[NSFontAttributeName] = self.font;
    dictM[NSForegroundColorAttributeName] = [UIColor redColor];
    CGPoint point = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5);

    [self.placeholder drawAtPoint:point withAttributes:dictM];
}
- (void)awakeFromNib
{
    DXLog(@"%@",self.subviews);
}
// 打印结果发现为空
// 猜测UITextField控件是懒加载的,调用这个方法的时候,它还没有加载storyboard/xib中的控件
// 利用GCD延时设置-->等storyboard/xib中的控件加载完毕在调用这个方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        DXLog(@"%@",self.subviews);
    });
// 打印结果是数组,里面有`UITextFieldLabel`,好像没有多大用

利用运行时,查看UITextField中内部的结构

// 这个方法调用时刻:当一个控件从xib或者storyBoard中创建之前,就会调用这个方法
- (void)awakeFromNib
{
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList([UITextField class], &count);
    for (int i = 0; i<count; i++) {
        Ivar ivar = ivarList[i];
        DXLog(@"%s",ivar_getName(ivar));
    }
    free(ivarList);
}
// 结果一大堆属性,其中有_placeholderLabel
// 利用KVC设置它颜色,结果成功
 UILabel *label = [self valueForKeyPath:@"_placeholderLabel"];
 label.textColor = [UIColor orangeColor];

方法二:有了placeholderLabel.textColor这个属性,在storyboard/xib中利用KVC设置placeholder颜色,如图


placeholder.png

方法三:用 setAttributedPlaceholder方法

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
[self setAttributedPlaceholder:attribute];

下面用四种方法,动态的设置placeholderLabel的颜色

方法一:addTarget

// 为了书写方便,便于管理,定义一个不可变的全局变量;
static NSString * const DXPlaceholderColorKey = @"placeholderLabel.textColor";
- (void)awakeFromNib
{
    [self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
}
    - (void)editingDidBegin
{
    [self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
}

- (void)editingDidEnd
{
    [self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}

方法二:代理-->遵守协议,自己做自己的代理

- (void)awakeFromNib
{
    self.delegate = self;
}
#pragma mark -------- <UITextFieldDelegate>---------
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}

方法三:通知

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidBegin) name:UITextFieldTextDidBeginEditingNotification object:self];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidEnd) name:UITextFieldTextDidEndEditingNotification object:self];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)editingDidBegin
{
    [self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
}

- (void)editingDidEnd
{
    [self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}

方法四:重写文本框特有的方法

- (void)awakeFromNib
{
    [self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}
- (BOOL)becomeFirstResponder
{
    [self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{
    [self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
    return [super resignFirstResponder];
}

 

 
原文地址:https://www.cnblogs.com/fengmin/p/5314500.html