[英]UITextField Placeholder Color
Im fully aware that there are multiple answers already on this. 我完全意识到,已经有多个答案。 I have added run time attributes.
我添加了运行时属性。 I have also added a UITextField extension.
我还添加了UITextField扩展。 The placeholder color still refuses to change on simulator and device.
占位符颜色仍然拒绝在模拟器和设备上更改。 I am not setting the placeholder through storyboard.
我不是通过情节提要设置占位符。 Placeholder text is being set by retrieving data from database.
正在通过从数据库检索数据来设置占位符文本。 Why won't my placeholder color change?
为什么我的占位符颜色不会更改?
extension UITextField{
@IBInspectable var placeHolderColor: UIColor? {
get {
return self.placeHolderColor
}
set {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[kCTForegroundColorAttributeName as NSAttributedStringKey: newValue!])
}
}
}
func loadUserInfo(){
if let username = user.name{
self.nameLabel.text = username
self.nameTextField.placeholder = username
print(username)
}
You have a few problems. 你有几个问题。
You call self.nameTextField.placeholder = username
. 您调用
self.nameTextField.placeholder = username
。 This wipes out any attributed placeholder you may have in place and it goes back to the default color. 这会清除掉您可能拥有的所有属性占位符,并恢复为默认颜色。 You would need to set
placeHolderColor
after setting placeholder
in order to see the color. 您需要在设置
placeholder
之后设置placeHolderColor
才能看到颜色。
The get
for your placeHolderColor
computed property is going to cause infinite recursion since it calls self.placeHolderColor
which calls the getter which calls self.placeHolderColor
which calls the getter... 您的
placeHolderColor
计算属性的get
将导致无限递归,因为它调用self.placeHolderColor
,后者调用getter,而getter调用self.placeHolderColor
,后者调用getter。
在获取用户名后的loadUserInfo中,我添加了self.nameTextField.attributedPlaceholder = NSAttributedString(string:user.name, attributes: [NSAttributedStringKey.foregroundColor: UIColor.green])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.