[英]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.