How do I change the placeholder text color of a UITextField through Swift? I can't seem to access the placeholder attribute. Thanks
You can set the placeholder text using an Attributed string . Set color to attributes
Property.
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text",
attributes:[NSForegroundColorAttributeName: UIColor.yellowColor()])
Swift 5
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text", attributes:[NSAttributedString.Key.foregroundColor: UIColor.yellow])
在 swift 中,您可以使用代码更改占位符颜色,
name_textField .setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")
You can create Subclass of Textfield and can override DrawRect function of textfield. By creating subclass you can set that for every textfields Easily.
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.