I'd like to change the color of the placeholder text I set in my UITextField
controls, to make it black.
I'd prefer to do this without using normal text as the placeholder and having to override all the methods to imitate the behaviour of a placeholder.
I believe if I override this method:
- (void)drawPlaceholderInRect:(CGRect)rect
then I should be able to do this. But I'm unsure how to access the actual placeholder object from within this method.
<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.
So - Bootstrap 3 defines the default placeholder color as #999 . (For reference, Bootstrap 4 defines the default placeholder color as #636c72 ).
Since the introduction of attributed strings in UIViews in iOS 6, it's possible to assign a color to the placeholder text like this:
if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) { UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}]; } else { NSLog(@"Cannot set placeholder text's color, because deployment target is earlier than iOS 6.0"); // TODO: Add fall-back code to set placeholder color. }
Easy and pain-free, could be an easy alternative for some.
_placeholderLabel.textColor
Not suggested for production, Apple may reject your submission.
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