16

I have a UITextField with a placeholder. When the user wants to submit the form and he/she hasn't typed anything in the textfield, I would like the placeholder's text color become red. Here are my questions:

  • Would that go against Apple's User interface guidelines? I don't want my app to be rejected because of such small detail.
  • How I would do it?

I know I can override the method drawPlaceholderInRect: in a subclass of UITextField. But if I did this, the text would be always red and as I wrote before, I would like it to become red depending on a user defined action.

The only solution I can think of is to use a "default" text for my UITextField (the placeholder's text), display it in light grey as long as the user hasn't typed anything and display it in red when I need it. In other words, I would just mock the placeholder's behavior. But of course, this is not very elegant.

Any ideas?

Venk
  • 5,949
  • 9
  • 41
  • 52
strave
  • 1,491
  • 2
  • 16
  • 26

8 Answers8

49

Just look at this:

Digdog Dig - Change UITextField’s placeholder color without subclassing it

[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
verklixt
  • 693
  • 5
  • 13
  • Simple solution for an attribute that should be changeable in Xcode, thank you! – FreeAppl3 Jul 13 '12 at 08:45
  • Oddly enough it is important to note that this must be set *after* setting the placeholder text itself or the change doesn't take effect. – shawnwall Sep 26 '12 at 17:07
  • 11
    This might work for now, but keep in mind that _because_ this is not public API, it might change without notice. Apple could also reject this for using private API. – Bob Vork Dec 04 '12 at 13:05
  • 2
    This solution should absolutely not be marked as the answer because what Bob mentiod, it is part of the private functions/values and your app can be rejected for this. You should never use private api's unless you are not going to distribute via the app store. – Saren Inden Nov 28 '13 at 10:55
26

You can Change the Placeholder textcolor to any color by using the below code. Just try this.

UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];
Atanu Mondal
  • 1,714
  • 1
  • 24
  • 30
12

like the answer from verklixt but without accessing private api and using UIAppearance:

[[UILabel appearanceWhenContainedIn:[UITextField class], nil] setTextColor:[UIColor darkGrayColor]];

(tested on 5.0 through 7.1)

glasz
  • 2,526
  • 25
  • 24
5

You can set the placeholder text as a NSAttributedString using this property

NSAttributedString *coloredPlaceholder = [[NSAttributedString alloc] initWithString:@"I     am a placeholder string" attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
[self.textField setAttributedPlaceholder:coloredPlaceholder];
Matt
  • 51
  • 2
  • 5
5

We can gain access to place holder label using key path,so that we can change color i.e.:

[self.textField setValue:[UIColor **"your color"**] 
                                  forKeyPath:@"_placeholderLabel.textColor"];
Eshwar Chaitanya
  • 697
  • 10
  • 21
4

override

-(void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor darkGrayColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:15.0]];
}
harshalb
  • 6,012
  • 13
  • 56
  • 92
  • 2
    The OP explicitly says they don't want to override `drawPlaceholderInRect:`. –  Jul 27 '12 at 08:40
3

when user does not write any thing in textfield. then put this text as a textfield.text text and change font color.

Rahul Juyal
  • 2,124
  • 1
  • 16
  • 33
  • Which text? The placeholder text? – strave Sep 07 '11 at 11:07
  • yes, just write the placeholder text as a normal text in textfield. – Rahul Juyal Sep 07 '11 at 11:35
  • 1
    Well, it's not what I expected but that will surely work. Thanks! – strave Sep 07 '11 at 12:00
  • 4
    I must say I don't like this kind of gotcha's, because You have to do more code by implementing the placeholder behavior and you always have to keep in mind the .text is not nil (and check if it's equal to default text, etc., etc.). What about subclassing as strave mentioned, but making the placeholder fill color depending on some variable, for example showRequired, and in setter setShowRequired: call [self setNeedsDisplay];, which would automaticaly do the redrawing of placeholder (self.showRequired?[[UIColor redColor] setFill]:[[UIColor grayColor] setFill])? – JakubKnejzlik May 24 '13 at 00:49
0

I had some difficulty implementing color change of placeholder, instead I've found another solution which works perfectly for me.

//On button action change color to red
-(void)btnAction
{
    // authentication for missing textfields
    if ([textField_firstName.text isEqualToString:@""])
    {
        textField_firstName.textColor=[UIColor redColor];
        textField_firstName.text=@"Enter First Name";
    }
}

// in the delegate method of UITextField change the following 
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //match the following string with above string and change the string to empty string on textfield click
    if ([textField_firstName.text isEqualToString:@"Enter First Name" ]) 
    {
        textField_firstName.text=@"";
    }

    //change back to the text color you use
    textField_firstName.textColor=[UIColor blackColor];
}
Arshad Parwez
  • 1,563
  • 2
  • 19
  • 29