1

I want to change the UItextfield placeholder color in objective c but my class is subclass of uiviewcontroller. can i inherit uiviewcontroller and uitextfield?

nuttekar
  • 25
  • 6
  • Here is remedy for your question [http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color][1] [1]: http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – Iducool Jul 19 '12 at 08:08

3 Answers3

1

Swift 4.0 and above

let text = textField.placeholder ?? ""
self.attributedPlaceholder = NSAttributedString(string: text, attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])

Objective-C

NSString *text = textField.text;
if (!text) {
    text = @"";
}
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];

Old Objective-C Code

[self.textfield setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor”];
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • hey it will work .... if you just want to change your textfield placeholder color... just replace your textField name with self.Textfield.. – TheTiger Jul 19 '12 at 07:59
  • hey, Textfield means it's global object, and my textfield is local – nuttekar Jul 19 '12 at 08:01
  • This might work but I don't know if Apple will accept it into the App Store because it's not a documented property of `UITextField`. Just something to be wary of. It might be safer to go with subclassing. – Anshu Chimala Jul 19 '12 at 08:04
  • just copy this line and paste under the declaration of your texfield and replace the name of textfield with your textfield. just like "[yourtextfield setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor”];" – Iducool Jul 19 '12 at 08:05
  • @AnshuChimala: you are correct. I have posted exact answer in comment. – Iducool Jul 19 '12 at 08:09
  • Broken in iSO13 it seems. – Albert Renshaw Oct 21 '19 at 01:54
  • 1
    @AlbertRenshaw Now you don't need to use this old code. You can simply set the `attributedPlaceholder` to `textField`. – TheTiger Oct 21 '19 at 05:50
  • @AnshuChimala Updated the answer. – TheTiger Oct 21 '19 at 05:56
  • @AlbertRenshaw I wanted to tag you but tagged AnshuChimla by mistake. Updated the answer with new code. – TheTiger Oct 22 '19 at 05:26
0

You need to subclass UITextField and ovveride the drawPlaceholderInRect method

look at this question

Community
  • 1
  • 1
Eyal
  • 10,777
  • 18
  • 78
  • 130
0

You can use bellow code

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