Change of UITextField placeholder color

后端 未结 13 1553
别跟我提以往
别跟我提以往 2021-01-30 06:09

How to dynamically change placeholder color of the UITextField? This is always the same system color.

No option in xib editor.

13条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 06:42

    First add this extension

    extension UITextField{
        @IBInspectable var placeHolderTextColor: UIColor? {
            set {
                let placeholderText = self.placeholder != nil ? self.placeholder! : ""
                attributedPlaceholder = NSAttributedString(string:placeholderText, attributes:[NSForegroundColorAttributeName: newValue!])
            }
            get{
                return self.placeHolderTextColor
            }
        }
    }
    

    Then you can change placeholder text color via storyboard or by just setting it like this :

    textfield.placeHolderTextColor = UIColor.red
    

提交回复
热议问题