Reputation: 107
How do I change the placeholder text color of a UITextField through Swift? I can't seem to access the placeholder attribute. Thanks
Upvotes: 6
Views: 17718
Reputation: 1261
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])
Upvotes: 18
Reputation: 512
swift 5
text.attributedPlaceholder = NSAttributedString(string: "your holder text", attributes: [NSAttributedString.Key.foregroundColor : textHolderColor])
Upvotes: 1
Reputation: 4551
Swift 4:
emailTextField.attributedPlaceholder = NSAttributedString(string: "e-mail", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
Upvotes: 2
Reputation: 209
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()])
} }
Upvotes: 2
Reputation: 3820
Click on the + button and add a new runtime attribute: _placeholderLabel.textColor
Run your app.
Upvotes: 6
Reputation: 7963
In swift you can change the placeholderColor by using the code,
name_textField .setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")
Upvotes: 3