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

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

How to change UITextfield placeholder & fontsize in SWIFT 2.0?

10条回答
  •  鱼传尺愫
    2020-12-28 16:49

    A simple solution is override placeholder property in an UITextField extension. It will update color of placeholder whole project. You don't need to update your code in many places.

    extension UITextField {
      var placeholder: String? {
          get {
              attributedPlaceholder?.string
          }
    
          set {
              guard let newValue = newValue else {
                  attributedPlaceholder = nil
                  return
              }
    
              let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: Color.textFieldPlaceholder.color]
    
              let attributedText = NSAttributedString(string: newValue, attributes: attributes)
    
              attributedPlaceholder = attributedText
          }
      }
    }
    
    

提交回复
热议问题