繁体   English   中英

如何在ios中使用子类更改UITextfield占位符textcolor

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

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