【问题标题】:How to change the UITextfield placeholder textcolor using subclass in ios如何在 ios 中使用子类更改 UITextfield 占位符文本颜色
【发布时间】:2015-12-29 06:21:50
【问题描述】:

我有一个带有占位符文本集的 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];

}

那么如何改变占位符的文字颜色。

【问题讨论】:

    标签: ios objective-c iphone xcode


    【解决方案1】:

    假设您的UITextField 子类是CustomUITextFieldPlaceholderAppearance

    只替换你的行:

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

    与:

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

    【讨论】:

      【解决方案2】:

      您可以设置用户定义运行时属性来更改文本字段占位符颜色。

      【讨论】:

        猜你喜欢
        • 2010-11-23
        • 2012-04-12
        • 2014-06-12
        • 1970-01-01
        • 2013-05-12
        • 2011-09-03
        • 1970-01-01
        • 2011-11-11
        • 2019-12-09
        相关资源
        最近更新 更多