簡體   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