如何动态更改UITextField
的占位符颜色?这始终是相同的系统颜色。
在xib编辑器中没有选项。
发布于 2014-01-12 12:45:57
来自文档的
属性@
(非原子,复制) NSAttributedString *属性占位符
默认情况下,此属性为nil。如果设置,则使用70%的灰色和属性字符串的其余样式信息(文本颜色除外)绘制占位符字符串。为此属性分配新值也会将占位符属性的值替换为相同的字符串数据,尽管没有任何格式信息。为此属性指定新值不会影响文本字段的任何其他与样式相关的属性。
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
Swift 4
let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str
发布于 2015-04-29 21:35:24
_placeholderLabel.textColor
swift中的
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])
Objective-C
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
P.S从Stackoverflow复制了3个不同的答案。
发布于 2014-01-13 04:39:33
使用下面的代码
[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
https://stackoverflow.com/questions/21074417
复制相似问题