ios 以编程方式更改 UITextField 的占位符文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7331604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change UITextField's placeholder text color programmatically
提问by strave
I have a UITextField
with a placeholder. When the user wants to submit the form and he/she hasn't typed anything in the textfield, I would like the placeholder's text color become red. Here are my questions:
我有UITextField
一个占位符。当用户想要提交表单并且他/她没有在文本字段中输入任何内容时,我希望占位符的文本颜色变为红色。以下是我的问题:
- Would that go against Apple's User interface guidelines? I don't want my app to be rejected because of such small detail.
- How I would do it?
- 这会违反 Apple 的用户界面指南吗?我不希望我的应用因为这么小的细节而被拒绝。
- 我会怎么做?
I know I can override the method drawPlaceholderInRect:
in a subclass of UITextField
. But if I did this, the text would be always red and as I wrote before, I would like it to become red depending on a user defined action.
我知道我可以drawPlaceholderInRect:
在UITextField
. 但是如果我这样做,文本将始终为红色,正如我之前所写,我希望它根据用户定义的操作变为红色。
The only solution I can think of is to use a "default" text for my UITextField
(the placeholder's text), display it in light grey as long as the user hasn't typed anything and display it in red when I need it. In other words, I would just mock the placeholder's behavior. But of course, this is not very elegant.
我能想到的唯一解决方案是为我的UITextField
(占位符的文本)使用“默认”文本,只要用户没有输入任何内容就以浅灰色显示,并在我需要时以红色显示。换句话说,我只会嘲笑占位符的行为。但当然,这不是很优雅。
Any ideas?
有任何想法吗?
采纳答案by Ron
when user does not write any thing in textfield. then put this text as a textfield.text text and change font color.
当用户没有在文本字段中写任何东西时。然后将此文本作为 textfield.text 文本并更改字体颜色。
回答by verklixt
Just look at this:
看看这个:
Digdog Dig - Change UITextField's placeholder color without subclassing it
Digdog Dig - 更改 UITextField 的占位符颜色而不将其子类化
[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
回答by Atanu Mondal
You can Change the Placeholder textcolor to any color by using the below code. Just try this.
您可以使用以下代码将占位符文本颜色更改为任何颜色。试试这个。
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];
回答by glasz
like the answer from verklixt but without accessing private api and using UIAppearance
:
就像 verklixt 的答案一样,但没有访问私有 api 并使用UIAppearance
:
[[UILabel appearanceWhenContainedIn:[UITextField class], nil] setTextColor:[UIColor darkGrayColor]];
(tested on 5.0 through 7.1)
(在 5.0 到 7.1 上测试)
回答by Matt
You can set the placeholder text as a NSAttributedString using this property
您可以使用此属性将占位符文本设置为 NSAttributedString
NSAttributedString *coloredPlaceholder = [[NSAttributedString alloc] initWithString:@"I am a placeholder string" attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
[self.textField setAttributedPlaceholder:coloredPlaceholder];
回答by Eshwar Chaitanya
We can gain access to place holder label using key path,so that we can change color i.e.:
我们可以使用键路径访问占位符标签,以便我们可以更改颜色,即:
[self.textField setValue:[UIColor **"your color"**]
forKeyPath:@"_placeholderLabel.textColor"];
回答by harshalb
override
覆盖
-(void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor darkGrayColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:15.0]];
}
回答by Arshad Parwez
I had some difficulty implementing color change of placeholder, instead I've found another solution which works perfectly for me.
我在实现占位符的颜色更改时遇到了一些困难,但我找到了另一个对我来说非常适合的解决方案。
//On button action change color to red
-(void)btnAction
{
// authentication for missing textfields
if ([textField_firstName.text isEqualToString:@""])
{
textField_firstName.textColor=[UIColor redColor];
textField_firstName.text=@"Enter First Name";
}
}
// in the delegate method of UITextField change the following
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//match the following string with above string and change the string to empty string on textfield click
if ([textField_firstName.text isEqualToString:@"Enter First Name" ])
{
textField_firstName.text=@"";
}
//change back to the text color you use
textField_firstName.textColor=[UIColor blackColor];
}