I want to change the UItextfield placeholder color in objective c but my class is subclass of uiviewcontroller. can i inherit uiviewcontroller and uitextfield?
Asked
Active
Viewed 849 times
3 Answers
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
-
-
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
-
-
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
-
-
@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 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