I have a design that implements a dark blue UITextField
, as the placeholder text is by default a dark grey colour I can barely make out what the place holder text says.
I've googled the problem of course but I have yet to come up with a solution while using the Swift language and not Obj-c.
Is there a way to change the placeholder text colour in a UITextField
using Swift?
You can set the placeholder text using an attributed string. Just pass the color you want to the attributes parameter.
<input type="text" placeholder="A red placeholder text..">
The easiest method to modify the placeholder text color is through the Xcode storyboard interface builder. Select the UITextField of interest and open the identity inspector on the right. Click on the plus symbol in the User Defined Runtime Attributes and add a new row with Key Path as placeholderLabel.
You can set the placeholder text using an attributed string. Just pass the color you want to the attributes
parameter.
Swift 5:
let myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30)) myTextField.backgroundColor = .blue myTextField.attributedPlaceholder = NSAttributedString( string: "Placeholder Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white] )
Swift 3:
myTextField.attributedPlaceholder = NSAttributedString( string: "Placeholder Text", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white] )
Older Swift:
myTextField.attributedPlaceholder = NSAttributedString( string: "Placeholder Text", attributes: [NSForegroundColorAttributeName: UIColor.white] )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With