如何动态更改占位符的颜色UITextField
?这始终是相同的系统颜色。
xib编辑器中没有选项。
如何动态更改占位符的颜色UITextField
?这始终是相同的系统颜色。
xib编辑器中没有选项。
Answers:
从文档
@property(非原子的,副本)NSAttributedString * attributedPlaceholder
默认情况下,此属性为nil。如果设置,则占位符字符串将使用70%的灰色和属性字符串的其余样式信息(文本颜色除外)绘制。为该属性分配新值还将用相同的字符串数据替换占位符属性的值,尽管没有任何格式信息。为该属性分配新值不会影响文本字段的任何其他与样式相关的属性。
物镜
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
斯威夫特4
let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ UITextAttributeTextColor : [UIColor redColor] }];
_placeholderLabel.textColor
迅速
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])
物镜
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
PS从Stackoverflow复制了3个不同的答案。
_placeholderLabel
:一些用户报告的App Store拒绝:stackoverflow.com/questions/1340224/...
使用以下代码
[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
首先添加此扩展
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
我在SWIFT中使用它:
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
看来这对其他人有用...我不知道为什么以前对我没用...也许某些项目设置。感谢您的评论。目前,我无法再次进行测试。
已淘汰: 但是我不知道为什么,文本正确应用,但占位符颜色保持不变(黑色/灰色)。
--iOS8
尝试这个:
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;
此解决方案无需任何子类,也没有任何私有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
}
}
}
@DogCoffee在Swift中的答案是
let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
textField.attributedPlaceholder = placeholder
这是上面@Medin Piranej提供的扩展的改进版本(顺便说一句好主意!)。如果您尝试获取placeHolderTextColor,则此版本避免了无休止的循环;如果颜色集为nil,则避免崩溃。
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 ?? "")
}
}
}
}