Ios – How to set the color of the place holder text for a UITextField while preserving its existing properties


I have seen some answers that show how to change the placeHolder text color for UITextField by overriding the drawPlaceholderInRect: method such as this one:

iPhone UITextField – Change placeholder text color

but that does not maintain the existing attributes such as alignment, font, etc…what is a better way to solve this?

Best Solution

From iOS 6,

Without any subclassing, one can accomplish this with a couple lines of code like so:

UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
Related Question