How to change UITextfield placeholder color and fontsize using swift 2.0?

前端 未结 10 687
小蘑菇
小蘑菇 2020-12-28 16:22

How to change UITextfield placeholder & fontsize in SWIFT 2.0?

10条回答
  •  北海茫月
    2020-12-28 16:54

    It's easy to do with a subclass of UITextField.

    Add placeholderColor property to easily set the color, and then observer changing of .placeholder to apply the color to it (with use of .attributedPlaceholder property)

    var placeholderColor: UIColor = .lightGray
    
    override var placeholder: String? {
        didSet {
            let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
            attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
        }
    }
    

    You do need to set the placeholder text programatically for the color to apply.

提交回复
热议问题