【发布时间】:2014-01-31 04:52:15
【问题描述】:
如何动态更改UITextField 的占位符颜色?
这始终是相同的系统颜色。
xib 编辑器中没有选项。
【问题讨论】:
-
在什么情况下你要改什么颜色?
-
当 UITextField 被禁用时
标签: ios uitextfield
如何动态更改UITextField 的占位符颜色?
这始终是相同的系统颜色。
xib 编辑器中没有选项。
【问题讨论】:
标签: ios uitextfield
来自文档
@property(nonatomic, copy) NSAttributedString *attributedPlaceholder
此属性默认为 nil。如果设置,占位符字符串是 使用 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
斯威夫特 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()])
Objective-C
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
P.S 从 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()])
似乎这对其他人有用......我不知道为什么它以前不适合我......也许是一些项目设置。感谢cmets。目前我没有办法再次测试它。
已过时: 但我不知道为什么,文本应用正确,但占位符颜色保持不变(黑色/灰色)。
--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;
【讨论】:
您可以使用以下代码
[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
【讨论】:
此解决方案无需任何子类化且无需任何私有 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}];
【讨论】:
@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 ?? "")
}
}
}
}
【讨论】:
对于 swift 3,我们可以使用此代码更改 UITextfield 的占位符文本颜色
let placeholderColor = UIColor.red
mytextField.attributedPlaceholder = NSAttributedString(string: mytextField.placeholder, attributes: [NSForegroundColorAttributeName : placeholderColor])
【讨论】:
斯威夫特 4
let placeholderColor = UIColor.red
self.passwordTextField?.attributedPlaceholder =
NSAttributedString(string:"placeholderText", attributes:
[NSAttributedStringKey.foregroundColor : placeholderColor])
【讨论】: