简体   繁体   中英

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.

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)];

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM