我该如何通过Swift更改UITextField的占位符文本颜色?我似乎无法访问占位符属性。谢谢
您可以使用属性字符串设置占位符文本。将颜色设置为属性
属性。
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text",
attributes:[NSForegroundColorAttributeName: UIColor.yellowColor()])
Swift 5
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text", attributes:[NSAttributedString.Key.foregroundColor: UIColor.yellow])
name_textField .setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")
Swift 4:
emailTextField.attributedPlaceholder = NSAttributedString(string: "e-mail", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
class CustomTextfield :UITextField {
override func drawRect(rect: CGRect) {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: UIColor.grayColor()])
} }
Swift 5
text.attributedPlaceholder = NSAttributedString(string: "your holder text", attributes: [NSAttributedString.Key.foregroundColor : textHolderColor])