使用用户属性更改 UITextField 的占位符文本颜色?

Posted

技术标签:

【中文标题】使用用户属性更改 UITextField 的占位符文本颜色?【英文标题】:Change UITextField's placeholder text color using Userattributes? 【发布时间】:2014-01-16 07:43:02 【问题描述】:

我想从用户属性(界面构建器)更改占位符颜色,因为我不想子类化我的 UITextfield 等...我在互联网上搜索并找到了这个“_placeholderLabel.textColor”是的,这在编程上很有效[self.tfEmail setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; 但是我想在界面生成器的用户属性中使用它。

【问题讨论】:

【参考方案1】:

尝试删除“_”,

为我工作。

【讨论】:

大声笑我从来没有机会在界面生成器中使用这样的占位符!啊哈太棒了,我想知道你还能用同样的方法创造什么 简直太棒了..!!好朋友。 您可以更改字体,您可以更改任何存在的属性。无需以编程方式进行。 @Xeieshan,是只用于 UITextField 还是任何控件? 你们俩wannabegeek.com/?p=395 blog.gaeremynck.com/user-defined-runtime-attributes-in-ios 供您参考。【参考方案2】:

不使用用户属性

 self.txtField.attributedPlaceholder = PlaceHolderAttributedString(@"ABCDEF");

其中 PlaceHolderAttributedString 是一个宏定义为

 #define PlaceHolderAttributedString(placeHolderText) [[NSAttributedString alloc] initWithString:placeHolderText attributes:@NSForegroundColorAttributeName:ColorTextFieldPlaceHolder]

使用用户属性

 placeholderLabel.textColor 

【讨论】:

【参考方案3】:

迅速:

    one_textfield.setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")

【讨论】:

以上是关于使用用户属性更改 UITextField 的占位符文本颜色?的主要内容,如果未能解决你的问题,请参考以下文章

将占位符添加到 UITextField,如何以编程方式快速设置占位符文本?

为啥 UITextview 没有 UITextField 之类的占位符属性 [重复]

如何使用swift 2.0更改UITextfield占位符颜色和fontsize?

如何更改 UITextField 的占位符颜色?

UITextField 无法将占位符居中并更改键盘颜色书写

UITextField 占位符问题