Changing Placeholder Text Color with Swift

前端 未结 30 2483
独厮守ぢ
独厮守ぢ 2020-11-29 15:57

I have a design that implements a dark blue UITextField, as the placeholder text is by default a dark grey colour I can barely make out what the place holder te

30条回答
  •  渐次进展
    2020-11-29 16:06

    Swift 3 (probably 2), you can override didSet on placeholder in UITextField subclass to apply attribute on it, this way:

    override var placeholder: String? {
    
        didSet {
            guard let tmpText = placeholder else {
                self.attributedPlaceholder = NSAttributedString(string: "")
                return
            }
    
            let textRange = NSMakeRange(0, tmpText.characters.count)
            let attributedText = NSMutableAttributedString(string: tmpText)
            attributedText.addAttribute(NSForegroundColorAttributeName , value:UIColor(white:147.0/255.0, alpha:1.0), range: textRange)
    
            self.attributedPlaceholder = attributedText
        }
    }
    

提交回复
热议问题