user3201089
user3201089

Reputation:

How to change the UITextfield placeholder textcolor using subclass in ios

I have a UITextField with placeholder text set. In the UITextField I want the placeholder text to red and also wants to change font family and fantasize.

So creating UITextField subclass. I am writing like this.

-(void)drawPlaceholderInRect:(CGRect)rect
{
[[UIColor redColor]setFill];
[[self placeholder]drawInRect:rect withFont:[UIFont fontWithName:@"verdana" size:15]];
}

next i am importing this class to my view controller class.

#import "SubViewController.h"
#import "CustomUITextFieldPlaceholderAppearance.h"

- (void)viewDidLoad 
{

[super viewDidLoad];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];

textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.delegate = self;
[self.view addSubview:textField];

}

Then how to change the placeholder text color.

Upvotes: 1

Views: 521

Answers (2)

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

Assume that your UITextField subclass is CustomUITextFieldPlaceholderAppearance

Only replace your line:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];

with:

CustomUITextFieldPlaceholderAppearance *textField = [[CustomUITextFieldPlaceholderAppearance alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];

Upvotes: 2

technerd
technerd

Reputation: 14514

You can set User Define Run time attributes to change textfield placeholder color.

enter image description here

Upvotes: 1

Related Questions