![](/img/trans.png)
[英]How do I subclass UITextField and override drawPlaceholderInRect to change Placeholder color
[英]How to change the UITextfield placeholder textcolor using subclass in ios
我有一个带占位符文本集的UITextField。 在UITextField中,我希望占位符文本为红色,并且还希望更改字体系列和幻想。
所以创建UITextField子类。 我写的是这样的。
-(void)drawPlaceholderInRect:(CGRect)rect
{
[[UIColor redColor]setFill];
[[self placeholder]drawInRect:rect withFont:[UIFont fontWithName:@"verdana" size:15]];
}
接下来我将此类导入到我的视图控制器类。
#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];
}
然后如何更改占位符文本颜色。
假设您的UITextField
子类是CustomUITextFieldPlaceholderAppearance
只更换你的线:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
有:
CustomUITextFieldPlaceholderAppearance *textField = [[CustomUITextFieldPlaceholderAppearance alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.