Avatar ·

How to change placeholder text color in UITextField using Swift?

Hello everyone! I'm a beginner in iOS development and can't figure out one seemingly simple task.

I have a UITextField with a placeholder. The default gray color doesn't match my app's design. I need the placeholder text to be, for example, blue or red.

I tried doing this:

textField.placeholder = "Enter name"\ntextField.placeholderColor = UIColor.blue // There is no such property, Xcode complains

Then I found a method online using NSAttributedString:

let placeholderText = NSAttributedString(string: "Enter name", attributes: [NSAttributedString.Key.foregroundColor: UIColor.blue])\ntextField.attributedPlaceholder = placeholderText

This worked