我有一个实现深蓝色的设计UITextField
,由于占位符文本默认为深灰色,我几乎看不清占位符文本的内容。
我当然已经用谷歌搜索了这个问题,但在使用 Swift 语言而不是 Obj-c 时我还没有找到解决方案。
有没有办法改变占位符文本颜色UITextField
使用斯威夫特?
您可以使用以下命令设置占位符文本属性字符串 https://developer.apple.com/documentation/foundation/nsattributedstring。只需将您想要的颜色传递给attributes
范围。
Swift 5:
let myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]
)
Swift 3:
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]
)
老斯威夫特:
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSForegroundColorAttributeName: UIColor.white]
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)