以编程方式更改UITextField的占位符文本颜色 [英] Change UITextField's placeholder text color programmatically

查看:112
本文介绍了以编程方式更改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.

这篇关于以编程方式更改UITextField的占位符文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆