I was making an app recently and in order to line up perfectly with designs, I needed the placeholder text in my UITextFields to be a certain colour, size & font.
After quite a bit of googling I couldn’t find my answer, so I made my own. Subclassing seemed a bit overkill, so I decided to create a category to override theĀ drawPlaceholderInRect method. See the code below:
UITextField+Placeholder.h
#import <UIKit/UIKit.h>
@interface UITextField (Placeholder)
- (void) drawPlaceholderInRect:(CGRect)rect;
@end
UITextField+Placeholder.m
#import <UITextField+Placeholder.h>
@implementation UITextField (Placeholder)
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor colorWithRed:142.0f/255.0f green:205.0f/255.0f blue:236.0f/255.0f alpha:1.0f] setFill];
[[self placeholder] drawInRect:rect withFont:[self font]];
}
@end
Then, in the files you wish to create UITextFields that have the custom Placeholders, simply import the header for your new category.
If you need to have several different UITextFields each with different placeholder colours then you’ll need to subclass.
The @interface and @implementation contents would be identical to if you used a Category.