以编程方式更改UITextField的占位符文本颜色
我有一个带占位符的 UITextField
。当用户想要提交表单并且他/她没有在文本字段中输入任何内容时,我希望占位符的文本颜色变为红色。以下是我的问题:
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:
- 这会违反Apple的用户界面指南吗?我不希望我的应用被拒绝,因为这么小的细节。
- 我该怎么办?
我知道我可以在 UITextField
的子类中覆盖 drawPlaceholderInRect:
方法。但是,如果我这样做,文本将始终是红色的,正如我之前写的那样,我希望它根据用户定义的操作变为红色。
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.
唯一的解决方案我可以想到的是为我的 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.
任何想法?
当用户没有在textfield中写任何东西时。然后将此文本作为textfield.text文本并更改字体颜色。
when user does not write any thing in textfield. then put this text as a textfield.text text and change font color.