更改UITextField占位符颜色 [英] Change of UITextField placeholder color
问题描述
如何动态更改 UITextField
的占位符颜色?
这总是相同的系统颜色。
How to dynamically change placeholder color of the UITextField
?
This is always the same system color.
xib编辑器中没有选项。
No option in xib editor.
推荐答案
来自文档
@property(非原子,复制)NSAttributedString * attributedPlaceholder
@property(nonatomic, copy) NSAttributedString *attributedPlaceholder
默认情况下,此属性为零。如果设置,占位符字符串是使用70%灰色绘制的
和属性字符串的剩余样式信息
(文本颜色除外)。为此属性分配新的
值也会使用相同的字符串数据替换占位符
属性的值,尽管没有任何格式化
信息。为此属性指定新值不会影响
文本字段的任何其他与样式相关的属性。
This property is nil by default. If set, the placeholder string is drawn using a 70% grey color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.
Objective-C
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }];
self.myTextField.attributedPlaceholder = str;
Swift
let str = NSAttributedString(string: "Text", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
myTextField.attributedPlaceholder = str
这篇关于更改UITextField占位符颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!