user2585611
user2585611

Reputation: 107

Change placeholder text color on swift

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

Answers (6)

iBhavin
iBhavin

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

Mohammed Abunada
Mohammed Abunada

Reputation: 512

swift 5

  text.attributedPlaceholder = NSAttributedString(string: "your holder text", attributes: [NSAttributedString.Key.foregroundColor : textHolderColor])

Upvotes: 1

Dani Pralea
Dani Pralea

Reputation: 4551

Swift 4:

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

Upvotes: 2

Bhavik
Bhavik

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

Shashi Verma
Shashi Verma

Reputation: 3820

enter image description here

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

Run your app.

Upvotes: 6

Vineesh TP
Vineesh TP

Reputation: 7963

In swift you can change the placeholderColor by using the code,

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

Upvotes: 3

Related Questions