Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change of UITextField placeholder color

How to dynamically change placeholder color of the UITextField? This is always the same system color.

No option in xib editor.

like image 522
theWalker Avatar asked Jan 12 '14 12:01

theWalker


People also ask

How do you change the placeholder color on a storyboard?

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.

How do I change placeholder color in Swift?

You can set the placeholder text using an attributed string. Just pass the color you want to the attributes parameter.

What is placeholder color code?

Note: In most browsers, the appearance of placeholder text is a translucent or light gray color by default.

How do I change the background color of a TextField in Swift?

TextField view doesn't provide a way to add background color but we can use the background modifier to add color to the TextField.


1 Answers

From Docs

@property(nonatomic, copy) NSAttributedString *attributedPlaceholder

This property is nil by default. If set, the placeholder string is drawn using a 70% grey color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.

Objective-C

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }]; self.myTextField.attributedPlaceholder = str; 

Swift

let str = NSAttributedString(string: "Text", attributes: [NSForegroundColorAttributeName:UIColor.redColor()]) myTextField.attributedPlaceholder = str 

Swift 4

let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red]) myTextField.attributedPlaceholder = str 
like image 100
DogCoffee Avatar answered Oct 13 '22 00:10

DogCoffee