Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITextField - Change placeholder text color

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.

like image 737
adam Avatar asked Aug 27 '09 10:08

adam


People also ask

How can I change the color of placeholder of text field?

<input type="text" placeholder="A red placeholder text..">

How do I 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.

What is placeholder color code?

So - Bootstrap 3 defines the default placeholder color as #999 . (For reference, Bootstrap 4 defines the default placeholder color as #636c72 ).


2 Answers

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. } 
like image 185
user1071136 Avatar answered Oct 11 '22 18:10

user1071136


Easy and pain-free, could be an easy alternative for some.

_placeholderLabel.textColor 

Not suggested for production, Apple may reject your submission.

like image 41
Jack Avatar answered Oct 11 '22 20:10

Jack