以编程方式设置 UITextField 占位符颜色 [英] Set UITextField placeholder color programmatically

查看:19
本文介绍了以编程方式设置 UITextField 占位符颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 swift 中以编程方式设置 UITextField 占位符颜色?

How to set UITextField place holder color programmatically in swift?

推荐答案

1 - 创建一个带有你想要的颜色的 AttributedString.
2 - 将此 AttributedString 设置为文本字段的属性占位符属性

1 - Create an AttributedString with colour you want.
2 - Set this AttributedString to the textfield attributedPlaceholder property

let placeholder = NSAttributedString(string: "Some", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 30));
textField.attributedPlaceholder = placeholder;
self.view.addSubview(textField)

来自苹果文档

变量属性占位符:NSAttributedString!
默认情况下,此属性为零.如果设置,占位符字符串将使用 70% 的灰色和属性字符串的剩余样式信息(文本颜色除外)绘制.为该属性分配一个新值也会用相同的字符串数据替换占位符属性的值,尽管没有任何格式信息.为该属性分配新值不会影响文本字段的任何其他与样式相关的属性.

var attributedPlaceholder: NSAttributedString!
This property is nil by default. If set, the placeholder string is drawn using a 70% grey color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.

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

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