0
votes

Im fully aware that there are multiple answers already on this. I have added run time attributes. I have also added a UITextField extension. 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?

enter image description here

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)
        }
2

2 Answers

1
votes

You have a few problems.

You call 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.

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...

0
votes

In loadUserInfo after getting users name I added self.nameTextField.attributedPlaceholder = NSAttributedString(string:user.name, attributes: [NSAttributedStringKey.foregroundColor: UIColor.green])