Ed Fernandez
Ed Fernandez

Reputation: 1823

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?

Upvotes: 20

Views: 31331

Answers (4)

ABS
ABS

Reputation: 7732

There are multiple ways to achieve this.

Using Interface Builder or Storyboard

  • Select the Textfield for which you want to change placeholder color
  • go to the Identity inspector menu on Top right of Xcode
  • Add the key value pair this way

Key path = _placeholderLabel.textColor

Click the Type and chose Color attribute .

Then select the color in value.

enter image description here

Set The placeholder color using code :

Process 1:

[textField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];

Process 2 :

Override drawPlaceholderInRect:(CGRect)rect method

- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:14]];
}

Upvotes: 22

Forrest
Forrest

Reputation: 620

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}];

Upvotes: 36

Ed Fernandez
Ed Fernandez

Reputation: 1823

There is indeed a much better way to handle this now. This will work for iOS 6 and 7.

(Note this example, I created the code in AwakeFromNib since it won't be changing colors once set. But if you don't use XIB, you will have to change the location where you put this code, such as in drawPlaceholderInRect,)

In this example, we create a subclass of UITextField, override awakeFromNib and then set the placeHolder text color to red:

- (void)awakeFromNib
{
    if ([self.attributedPlaceholder length])
    {
        // Extract attributes
        NSDictionary * attributes = (NSMutableDictionary *)[ (NSAttributedString *)self.attributedPlaceholder attributesAtIndex:0 effectiveRange:NULL];

        NSMutableDictionary * newAttributes = [[NSMutableDictionary alloc] initWithDictionary:attributes];

        [newAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];

        // Set new text with extracted attributes
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self.attributedPlaceholder string] attributes:newAttributes];

    }
}

The nice thing about this approach is that it maintains the current UITextField properties for the placeHolder string and so will allow you to work in IB for most of what you set. In addition, its much more efficient than doing everytime you need to draw. It also allows you to change any other property you want on the placeHolder text while maintaining the rest of the properties.

As mentioned above, if don't use XIBs, then you will need to call this at some other time. If you do put this code in the drawPlaceholderInRect: method, then make sure you call [super drawPlaceholderInRect:] at the end of it.

Upvotes: 15

Nazik
Nazik

Reputation: 8444

The safe way to customize UITextField’s placeholder is subclassing the UITextField and overriding placeholderRectForBounds:, Apple won’t bother you on this one. However, if you want to take the risk, you can try this way:

[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];

Source.

Upvotes: 3

Related Questions