回答
从文档
@属性(非原子,副本)NSAttributedString * attributedPlaceholder
此属性默认为零。如果设置,占位符字符串是 ,使用70%灰色和其余样式信息 (文本颜色除外)绘制属性字符串。为该属性分配新的 值也会将相同字符串数据的占位符 属性的值替换,尽管没有任何格式的 信息。为该属性分配新值不会影响文本字段的任何其他与样式相关的属性。
Objective-C的
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }];
self.myTextField.attributedPlaceholder = str;
下面的代码斯威夫特
let str = NSAttributedString(string: "Text", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
myTextField.attributedPlaceholder = str
我无法测试它,NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:myString];总是零。 – theWalker
你为什么要创建一个可变的属性字符串?只是:'NSAttributedString * str = [[NSAttributedString alloc] initWithString:@“Some Text”attributes:@ {UITextAttributeTextColor:[UIColor redColor]}];' – rmaddy
干杯@rmaddy,我使用NSForegroundColorAttributeName。 – DogCoffee
使用
[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
我在SWIFT使用:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
看来,这适用于其他...我不知道为什么它没有为我工作过......也许有些项目设置。感谢您的评论。目前我无法再如何测试它。
已过时: 但我不知道为什么,文本应用正确,但占位符颜色保持不变(黑色/灰色)。
--iOS8
这不是一个答案 –
是的,它不是(你的评论)。但是我不想开始一个新的线程,因为这个代码与swift中重写的obj-c代码是一样的。 – Tomino
我不知道为什么它不适合你。但它对我很好 –
试试这个:
NSAttributedString *strUser = [[NSAttributedString alloc] initWithString:@"Username" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
self.username.attributedPlaceholder = strUser;
self.password.attributedPlaceholder = strPassword;
使用代码标签! – gsamaras
您可以使用下面的代码
[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
@ DogCoffee在斯威夫特的答案是
let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
textField.attributedPlaceholder = placeholder
_placeholderLabel.textColor
在迅速
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])
目标C
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
P.S复制从#1 3个不同的答案。
'_placeholderLabel'的警告:有些用户报告App Store拒绝:http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color#comment56758204_22017127 – ftvs
此方法适用于没有任何的子类,没有任何私人的ivars:
@IBOutlet weak var emailTextField: UITextField! {
didSet {
if emailTextField != nil {
let placeholderText = NSLocalizedString("Tap here to enter", comment: "Tap here to enter")
let placeholderString = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor(white: 0.66, alpha: 1.0)])
emailTextField.attributedPlaceholder = placeholderString
}
}
}
尝试。
UIColor *color = [UIColor redColor];
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}];
首先添加此扩展
extension UITextField{
@IBInspectable var placeHolderTextColor: UIColor? {
set {
let placeholderText = self.placeholder != nil ? self.placeholder! : ""
attributedPlaceholder = NSAttributedString(string:placeholderText, attributes:[NSForegroundColorAttributeName: newValue!])
}
get{
return self.placeHolderTextColor
}
}
}
然后你就可以通过故事板或只设置像这样改变占位符文本颜色:
textfield.placeHolderTextColor = UIColor.red
[yourtextFieldName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
这是一个改进版本上面@Medin Piranej提供的扩展名(顺便说一句好主意!)。如果您尝试获取placeHolderTextColor并避免在颜色集为零时防止崩溃,此版本可避免无限循环。
public extension UITextField {
@IBInspectable public var placeholderColor: UIColor? {
get {
if let attributedPlaceholder = attributedPlaceholder, attributedPlaceholder.length > 0 {
var attributes = attributedPlaceholder.attributes(at: 0,
longestEffectiveRange: nil,
in: NSRange(location: 0, length: attributedPlaceholder.length))
return attributes[NSForegroundColorAttributeName] as? UIColor
}
return nil
}
set {
if let placeholderColor = newValue {
attributedPlaceholder = NSAttributedString(string: placeholder ?? "",
attributes:[NSForegroundColorAttributeName: placeholderColor])
} else {
// The placeholder string is drawn using a system-defined color.
attributedPlaceholder = NSAttributedString(string: placeholder ?? "")
}
}
}
}
- 1. 使用Userattributes更改UITextField的占位符文本颜色?
- 2. 如何更改UITextField的占位符颜色?
- 3. UISearchBar更改占位符颜色
- 4. 更改Braintree占位符文本颜色
- 5. jQuery更改占位符文本颜色
- 6. 如何更改输入占位符颜色javascript onmouseover,更改占位符值onmouseover?
- 7. 如何更改Xamarin for iOS7中的UITextField占位符的颜色和字体
- 8. UITextField更改占位符颜色,当它变成第一个响应者
- 9. 如何继承UITextField并覆盖drawPlaceholderInRect以更改占位符颜色
- 10. UITextField占位符字体颜色白色iOS 5?
- 11. 占位符颜色不会改变
- 12. 如何改变占位符颜色CSS
- 13. Firefox的占位符颜色改变
- 14. 更改占位符的最后一个字符的颜色
- 15. UITextField中占位符文本的默认颜色是什么?
- 16. 更改UITextField键盘的色调颜色
- 17. 更改内部类占位符的文本颜色
- 18. 在Javascript中更改占位符的颜色以适用于Chrome
- 19. 如何更改占位符文本的颜色
- 20. 如何更改javascript格式占位符的颜色?
- 21. 如何更改目标c占位符的颜色
- 22. 在动画中更改占位符颜色
- 23. 更改digitalBush/jquery.maskedinput中占位符的颜色
- 24. CSS - 无法通过分类更改占位符颜色
- 25. 角度材质2 - 更改md-select占位符颜色
- 26. 无法更改输入占位符的颜色
- 27. 在窗体中更改下拉框占位符的颜色
- 28. 如何更改默认的select2占位符颜色的CSS?
- 29. 如何使用javascript更改占位符的颜色?
- 30. 在验证文本字段时更改占位符颜色
在何种情况下,你要改变什么颜色? – Suryakant
当UITextField被禁用 – theWalker
重复?:[iPhone UITextField - 更改占位符文本颜色](http://stackoverflow.com/q/1340224/456814)。 – 2014-08-14 01:02:48