我有一个实现了深蓝色UITextField
的设计,因为占位符文本默认是深灰色的,我几乎不能理解占位符文本所说的内容。
当然,我已经用谷歌搜索了这个问题,但在使用Swift语言而不是Obj-c时,我还没有想出解决方案。
有没有办法使用Swift更改UITextField
中的占位符文本颜色?
发布于 2016-05-24 22:44:08
如下所示创建UITextField
扩展:
extension UITextField{
@IBInspectable var placeHolderColor: UIColor? {
get {
return self.placeHolderColor
}
set {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: newValue!])
}
}
}
在你的故事板或.xib中。你会看到
发布于 2017-02-11 01:35:31
在Swift 3.0中,使用
let color = UIColor.lightText
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder, attributes: [NSForegroundColorAttributeName : color])
在Siwft 5.0 +中使用
let color = UIColor.lightText
let placeholder = textField.placeholder ?? "" //There should be a placeholder set in storyboard or elsewhere string or pass empty
textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : color])
发布于 2016-09-27 12:14:29
下面的代码可以在Swift3中运行:
yourTextFieldName .setValue(UIColor.init(colorLiteralRed: 80/255, green: 80/255, blue: 80/255, alpha: 1.0), forKeyPath: "_placeholderLabel.textColor")
如果你有任何问题,请告诉我。
https://stackoverflow.com/questions/26076054
复制相似问题