8

How to to change the color of the placeholder text I set in my UITextField controls, to make it black?

  • 2
    Above all the answers, from iOS 6, there's a property `NSAttributedString *attributedPlaceholder` e.g. `textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:textField.font, NSForegroundColorAttributeName:textField.textColor}];` – Hemang Oct 08 '14 at 07:01

6 Answers6

44

using KVC

[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];

you can set your own colour to placeholder

Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • Has anyone submitted an app to the App Store with this? Seems to me like accessing `_placeholder` might count as a private method... – samvermette Feb 15 '14 at 21:24
  • @samvermette yes we submitted an app with this code , it will not get into as private method – Vinodh Feb 17 '14 at 06:06
  • 1
    See http://stackoverflow.com/a/3396065/309046 answer – Satish Nov 25 '14 at 08:09
  • i submitted an app with such code many times. Its clear, i did not have any problem – Yucel Bayram Aug 11 '15 at 12:54
  • This will work and is probably harmless, but there is already a mechanism to do this without calling a private getter. Why not just do it the "right" way referenced by @satish? – GnarlyDog Nov 17 '15 at 18:30
13

This is a best way to change your placeholder color via KVO...

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

I hope it helps you change placeholder color. Thanks

chandan
  • 2,453
  • 23
  • 31
7

You can override drawPlaceholderInRect:(CGRect)rect as such to manually render the placeholder text:

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

}

DD_
  • 7,230
  • 11
  • 38
  • 59
4

You will have to subclass the UITextField class and override the following method.

- (void) drawPlaceholderInRect:(CGRect)rect
{
     [[UIColor blackColor] setFill];

    [[self placeholder] drawInRect:rect withFont:[UIFont italicSystemFontOfSize:17] lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentLeft];

}
Xcoder
  • 1,762
  • 13
  • 17
1

You can use this to change the placeholder text color.

- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
Ahmed Z.
  • 2,329
  • 23
  • 52
1

Make use of the following code. which will help you.

 - (void) drawPlaceholderInRect:(CGRect)rect
   {
     [[UIColor redColor] setFill];
     [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
   }
Vinayak Kini
  • 2,919
  • 21
  • 35