Reputation: 499
I have tried the two options everybody answers in this forum but nothing works for me... I have tried to override:
- (void)drawPlaceholderInRect:(CGRect)rect
and also:
[self.myTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
but nothing happens... Still the same gray color... Maybe I have to inherit something, I don't know... Any suggestion?
Upvotes: 2
Views: 3637
Reputation: 885
the simplest way to do this:
UILabel *placeholderAppearance;
if (@available(iOS 9, *)) {
placeholderAppearance = [UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITextField class]]];
} else {
placeholderAppearance = [UILabel appearanceWhenContainedIn:[UITextField class], nil];
}
placeholderAppearance.textColor = [UIColor redColor];
[EDITED] After using this I got to know another bug, the above did the job but it also reset the color of all other labels in my view to Default Black. Then to overcome this, I had to sub class the UILabel class and use my class in all other labels in my view.
Upvotes: 4
Reputation: 8105
From iOS6 you can use the attributed placeholder:
textfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:textfield.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
Upvotes: 0
Reputation: 6413
See drawPlaceholderInRect:
of UITextField
. You need to subclass UITextField and override this method for configuring graphics context with desired text color and call super implementation.
By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or do the drawing yourself. If you do render the text yourself, you should not invoke super.
Upvotes: 1