Change of UITextField placeholder color

后端 未结 13 1527
别跟我提以往
别跟我提以往 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:57

    This solution works without any subclassing and without any private ivars:

    @IBOutlet weak var emailTextField: UITextField! {
        didSet {
            if emailTextField != nil {
                let placeholderText = NSLocalizedString("Tap here to enter", comment: "Tap here to enter")
                let placeholderString = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor(white: 0.66, alpha: 1.0)])
                emailTextField.attributedPlaceholder = placeholderString
            }
        }
    }
    

提交回复
热议问题