美文网首页
textFiled占位文字颜色

textFiled占位文字颜色

作者: 陈水寒 | 来源:发表于2017-01-09 13:01 被阅读49次

设置textFiled占位文字颜色,进入编辑的时候占位文字颜色变成白色
默认状态textField效果:

默认状态.gif

修改后的textFiled效果:


设置占位颜色后效果.gif

设置方法:
1、自定义一个textFiled类,继承自UITextFiled,将登陆界面上的文本框都修改成自定义类;
2、在自定义类的.m文件中实现以下方法即可:

- (void)awakeFromNib{
    /*
     1、 设置光标的颜色为白色
     2、 设置文本颜色为白色
     2、 占位文字颜色变成白色
     */
    self.tintColor = [UIColor whiteColor]; // 改变光标的颜色
    self.textColor = [UIColor whiteColor]; // 设置文本的颜色
    
    // 当文本框进入编辑状态的时候改变文字颜色
    [self addTarget:self action:@selector(textEditBegin) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(textEditEnd) forControlEvents:UIControlEventEditingDidEnd];
    
    // 初始化的时候先将占位文字设置成灰色,编辑的时候变成白色,退出编辑的时候再变回灰色
    // 占位颜色设置的2种方式:
    // 1.
//    NSMutableDictionary *attri = [NSMutableDictionary dictionary];
//    attri[NSForegroundColorAttributeName] = [UIColor grayColor];
//    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attri];
//    UITextField *label;
    // 2.
    UILabel *placeholderLabel = [self valueForKey:@"placeholderLabel"];
    placeholderLabel.textColor = [UIColor grayColor];
}

- (void)textEditBegin{
    // 1.
//    NSMutableDictionary *attri = [NSMutableDictionary dictionary];
//    attri[NSForegroundColorAttributeName] = [UIColor grayColor];
//    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attri];
//    UITextField *label;
    // 2.
    UILabel *placeholderLabel = [self valueForKey:@"placeholderLabel"];
    placeholderLabel.textColor = [UIColor whiteColor];
}

- (void)textEditEnd{
    // 1.
//    NSMutableDictionary *attri = [NSMutableDictionary dictionary];
//    attri[NSForegroundColorAttributeName] = [UIColor grayColor];
//    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attri];
//    UITextField *label;
    // 2.
    UILabel *placeholderLabel = [self valueForKey:@"placeholderLabel"];
    placeholderLabel.textColor = [UIColor grayColor];
}

相关文章

网友评论

      本文标题:textFiled占位文字颜色

      本文链接:https://www.haomeiwen.com/subject/zmxhbttx.html