【发布时间】:2014-12-29 04:25:31
【问题描述】:
如何通过 Swift 更改 UITextField 的占位符文本颜色?我似乎无法访问占位符属性。谢谢
【问题讨论】:
如何通过 Swift 更改 UITextField 的占位符文本颜色?我似乎无法访问占位符属性。谢谢
【问题讨论】:
您可以使用 属性字符串 设置占位符文本。将颜色设置为attributes
属性。
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text",
attributes:[NSForegroundColorAttributeName: UIColor.yellowColor()])
斯威夫特 5
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text", attributes:[NSAttributedString.Key.foregroundColor: UIColor.yellow])
【讨论】:
在 swift 中,您可以使用代码更改占位符颜色,
name_textField .setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")
【讨论】:
您可以创建Textfield的子类,并可以覆盖textfield的DrawRect函数。 通过创建子类,您可以轻松地为每个文本字段设置它。
class CustomTextfield :UITextField {
override func drawRect(rect: CGRect) {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: UIColor.grayColor()])
} }
【讨论】:
斯威夫特 4:
emailTextField.attributedPlaceholder = NSAttributedString(string: "e-mail", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
【讨论】:
斯威夫特 5
text.attributedPlaceholder = NSAttributedString(string: "your holder text", attributes: [NSAttributedString.Key.foregroundColor : textHolderColor])
【讨论】: