17

I have a 3 UITextField with placeholder text set. On one of the UITextField I want the placeholder text to be red.

Now after googling it seems the best way to do this is to subclass UITextField and override drawPlaceholderInRect.

How do I go about subclassing and overriding drawPlaceholderInRect? I've not found any code examples or tutorials on this and I'm new to objective-c and iOS development so finding it tricky to work it out.

Answer:

Created a new objective-c class called CustomUITextFieldPlaceholder which subclassed UITextField. In CustomUITextFieldPlaceholder.m put the following code

 @implementation CustomUITextFieldPlaceholder

- (void)drawPlaceholderInRect:(CGRect)rect {
    // Set colour and font size of placeholder text
    [[UIColor redColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
}

@end

To implement the above in your project

#import "CustomUITextFieldPlaceholder.h"

and

IBOutlet CustomUITextFieldPlaceHolder *txtName;

Note: This works and I believe is correct practice, however I have not fully tested it. Hope this example helps others in my situation.

Edit:

Changed

[[UIColor redColor] setFill];

for

[[UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:0.7] setFill];

So that I could set the opacity to 70% to mimic default placeholder.

Shmidt
  • 16,436
  • 18
  • 88
  • 136
Matt Price
  • 34,499
  • 6
  • 24
  • 33
  • Subclassing and overriding that method won't help you change the font colour, it just lets you customise where the placeholder is displayed. – Tom Irving May 27 '11 at 11:23
  • maybe you can try overriding this delegate method - `(void)drawPlaceholderInRect:(CGRect)rect` – Shrey May 27 '11 at 11:27

3 Answers3

8

To answer you specific question, this is how subclassing works:

// CustomTextField.h
@interface CustomTextField : UITextField {
}
@end

Here's how to override the method:

@implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return CGRectMake(x,y,width,height);
}
@end

However I don't think that's the method you want to override. I think this is what you're looking for:

@implementation
- (void)drawPlaceholderInRect:(CGRect)rect {
    // Your drawing code.
}
@end
Erik B
  • 40,889
  • 25
  • 119
  • 135
  • Thanks for your reply @Erik, your right i did mean drawPlaceholderInRect. I will have a go at creating the code to do this, fingers crossed :) – Matt Price May 27 '11 at 11:52
  • but after creating class and adding this method in implementation i cant got change on my textfield placeholder color – Hardik Vyas Apr 05 '16 at 05:55
0
[yourTextfield setValue:[UIColor colorWithRed:62.0/255.0f green:62.0/255.0f blue:62./255.0f alpha:1.0f]  forKeyPath:@"_placeholderLabel.textColor"];

I guess this would be helpful

Jasper
  • 7,031
  • 3
  • 35
  • 43
VIPIN
  • 1
0

Subclassing won't change the color. I have put forward a work around here.

UITextField placeholder font color white iOS 5?

Community
  • 1
  • 1
Jibran K
  • 885
  • 7
  • 20