Change the color of placeholder text to swift

How can I change the text color of a UITextField placeholder via Swift? I can't seem to access the placeholder attribute. thank

+3


source to share


5 answers


You can set the placeholder text using the Attributed string . Set the color to attributes

Property.



   textField.attributedPlaceholder = NSAttributedString(string:"placeholder text",
   attributes:[NSForegroundColorAttributeName: UIColor.yellowColor()])

      

+13


source


enter image description here

Click the + button and add a new execution attribute: _placeholderLabel.textColor



Run the application.

+5


source


In swift, you can change placeholderColor with code,

 name_textField .setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")

      

+3


source


You can subclass the textbox and override the DrawRect function of the textbox. By subclassing, you can easily set this for each text field.

class CustomTextfield :UITextField {
 override func drawRect(rect: CGRect) {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: UIColor.grayColor()])
} }

      

+1


source


Swift 4:

emailTextField.attributedPlaceholder = NSAttributedString(string: "e-mail", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])

      

0


source







All Articles