[英]Change placeholder text color on swift
如何通过 Swift 更改 UITextField 的占位符文本颜色? 我似乎无法访问占位符属性。 谢谢
您可以使用Attributed string设置占位符文本。 将颜色设置为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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.