UITextField set placeholder color as dark in iOS
Asked Answered
A

8

7

I have tried to set UITextField "Placeholder" color as dark.

NSAttributedString * search = [[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

textField.attributedPlaceholder = search;
  • But still UITextField showing light gray color in "Placeholder".

  • Is it possible to set dark "placeholder" color for UITextField?

Also I Have tried the another method as well

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

But both methods are working in iOS 7, But not working on iOS 6.

  • Is it possible to set dark "placeholder" color for UITextField in iOS 6 target?

Thanks!

Alligator answered 19/4, 2014 at 4:24 Comment(1)
The code you posted should work - it does for me. Make sure there is no call to set the regular placeholder property after this. Also make sure textField isn't nil when you call the code.Flak
V
20

As suggested by Apple, subclassing UITextField and overriding - (void)drawPlaceholderInRect:(CGRect)rect is the way to go:

- (void)drawPlaceholderInRect:(CGRect)rect { 
    UIColor *colour = [UIColor lightGrayColor]; 
    if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) 
    { // iOS7 and later 
        NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font}; 
        CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil]; 
        [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; } 
    else { // iOS 6 
        [colour setFill]; 
        [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment]; 
    } 
 } 

Credit: http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6

Valentinevalentino answered 2/5, 2014 at 6:37 Comment(1)
Works in iOS8 as well.Hedy
L
9

Overriding the drawPlaceholderInRect: method to draw our own placeholder text.

- (void)drawPlaceholderInRect:(CGRect)rect
{
    UIColor *colour = [UIColor darkColor];
    
    if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) {
        // iOS7 and later
        NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font};
        CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil];
        [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes];
    }
    else {
        // iOS 6
        [colour setFill];
        [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
    }
}

OR

This case, likely crash if the internal variable changes in the future

Programmatically:

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

In UIStoryBoard:

enter image description here

OR

EDIT: Playing with UITextFiled delegates. its like a tricky:

-(void)viewDidLoad{
  self.mytxtField.text = @"PlaceholderText";
  self.mytxtField.delegate = self;
   self.mytxtField.textColor = [UIColor darkGrayColor];

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
           if ([self.mytxtField.text isEqualToString: @"PlaceholderText"]) {
             self.mytxtField.text = @"";
             self.mytxtField.textColor = [UIColor blackColor]; 
        }
    }


-(void)textFieldDidEndEditing:(UITextField *)textField{
      if ([self.mytxtField.text length]<1) {
            self.mytxtField.text =@"PlaceholderText";
            self.mytxtField.textColor = [UIColor darkGrayColor]; 

        }  
La answered 19/4, 2014 at 4:26 Comment(8)
Will this feature has been changed in feature? I mean, Is it efficient way to customize UITextField?Alligator
what do you mean in the way of efficient.?La
It's not a good idea to use this approach. It accesses the private subview structure of the text field. It could fail in any future iOS update.Flak
So, What will be the efficient way to update the UITextField Placeholder color as dark? @FlakAlligator
@Flak : Can you plz, expand it It accesses the private subview structure of the text field.. even I'm not sure about it so..La
I mean exactly what I say. Calling setValue: with the key path of _placeholderLabel is a way to access the private ivar _placeholderLabel. You are not supposed to access private ivars. Apple could rename that ivar in iOS 8, for example. Then this code will stop working.Flak
Even worse than not working, the second solution will likely crash if the internal variable changes in the future, forcing you to release an update and hope your users update. The first solution is ok as it checks if the variable exists and in future releases would "fail silently".Cleave
@Rivera : Yes, I agree to you. How about the edited one.La
B
9

For iOS8, you can take advantage of @IBDesignable & @IBInspectable.

Here is a UITextField subclass in Swift which lets you set the placeholder color in Interface Builder and see the result instantly:

@IBDesignable class EDTextField: UITextField {
    @IBInspectable var placeholderColor: UIColor = UIColor.whiteColor() {
        didSet {
            if let placeholder = self.placeholder {
                let attributes = [NSForegroundColorAttributeName: placeholderColor]
                attributedPlaceholder = NSAttributedString(string: placeholder, attributes: attributes)
            }
        }
    }
}
Bellerophon answered 4/4, 2015 at 22:24 Comment(1)
How cool is that ... never heard of @IBDesignable and @IBInspectable before, thanks!Archlute
C
7

Its very simple....

Try this to change placeholder text color..

UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
Caphaitien answered 19/4, 2014 at 4:26 Comment(2)
Why use a different approach for iOS 6? iOS 6 supports the attributePlaceholder property.Flak
As I commented on the other answer, Method 2 will start crashing your app as soon as the implementation changes in a future iOS release.Cleave
E
1

Here is Latest way to Change FONT and COLOR of placeholder text

    self.emailField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Email" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName :[UIFont fontWithName:@"OpenSans" size:14.0]}];
Emf answered 17/1, 2016 at 17:52 Comment(0)
N
0

Try this

 [textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
Nerland answered 19/4, 2014 at 4:26 Comment(1)
This will cause an exception in iOS 13: *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug'Miquelmiquela
C
0

To make it future-proof and completely customizable (color, font, size, etc.), I would just add a UILabel on top and hide/show it manually according to the UITextField contents.

Just make sure to set the label's userInteractionEnabled to NO.

Cleave answered 30/4, 2014 at 1:56 Comment(2)
Hi, I would like to use iOS feature as textField.attributedPlaceholder. But this method is working in iOS 7, But not working on iOS 6. Thoughts?Alligator
I understand, but changing the placeholder's text color for instance is not an "iOS feature", a reason why I suggest using a customized UILabel instead. This is guaranteed to work on iOS 6/7/8+.Cleave
F
0

It will change textField's palceholder textColor

[yourTextField setValue:[UIColor colorWithRed:150.0f/255.0f green:150.0f/255.0f blue:155.0f/255.0f alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
Flatling answered 30/4, 2014 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.