美文网首页
textField 更改placeHolder字符大小、颜色等属

textField 更改placeHolder字符大小、颜色等属

作者: GodfansMa | 来源:发表于2016-09-05 14:56 被阅读36次

两种实现方式

  • 传统使用KVC实现
  • iOS6.0之后,有attributedPlaceholder属性,可以直接通过它更改

1.KVC实现

[self.titleTF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

说明

如何知道keyPath是 _placeholderLabel.textColor? 可以直接断点查看子view成员变量
Paste_Image.png

2.attributedPlaceholder 设置

@property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder NS_AVAILABLE_IOS(6_0);
//
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:holderText];
//
[placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, holderText.length)];
//
[placeholder addAttribute:NSFontAttributeName value:[UIFontboldSystemFontOfSize:16] range:NSMakeRange(0, holderText.length)];
//                
self.titleTF.attributedPlaceholder = placeholder;

自己踩得坑

在没有给textField.placeholder 赋值之前上面两种方法设置都是无效的,因为_placeholderLabel 是nil

Paste_Image.png

相关文章

网友评论

      本文标题:textField 更改placeHolder字符大小、颜色等属

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