Changing Placeholder Text Color with Swift

前端 未结 30 2514
独厮守ぢ
独厮守ぢ 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:15

    In my case, I use Swift 4

    I create extension for UITextField

    extension UITextField {
        func placeholderColor(color: UIColor) {
            let attributeString = [
                NSAttributedStringKey.foregroundColor: color.withAlphaComponent(0.6),
                NSAttributedStringKey.font: self.font!
                ] as [NSAttributedStringKey : Any]
            self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: attributeString)
        }
    }
    

    yourField.placeholderColor(color: UIColor.white)

提交回复
热议问题