首页 文章

使用Swift更改占位符文本颜色

提问于
浏览
151

我的设计实现了深蓝色 UITextField ,因为占位符文本默认为深灰色,我几乎无法确定占位符文本所说的内容 .

我当然用Google搜索了问题,但我还没有提出解决方案,而使用Swift语言而不是Obj-c .

有没有办法使用Swift更改 UITextField 中的占位符文本颜色?

20 回答

  • 3

    Xcode 9.2 Swift 4

    extension UITextField{
        @IBInspectable var placeHolderColor: UIColor? {
            get {
                return self.placeHolderColor
            }
            set {
                self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: newValue!])
            }
        }
    }
    
  • 0

    对于Swift 3和3.1,这非常好用:

    passField.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSForegroundColorAttributeName: UIColor.white])
    
  • 0

    对于 Swift 4.0, X-code 9.1 version or iOS 11 ,您可以使用以下语法来使用不同的占位符颜色

    textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
    
  • 2

    您可以使用属性字符串设置占位符文本 . 使用 attributes 传递所需的颜色:

    var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
    myTextField.backgroundColor = .blue
    myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                                 attributes: [NSForegroundColorAttributeName: UIColor.yellow])
    

    For Swift 3+ use following:

    myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                                 attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    

    For Swift 4.2 use following:

    myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                                 attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
    
  • 333

    您可以使用Interface Builder快速完成此操作,而无需添加任何代码 .

    选择 UITextField 并打开右侧的身份检查器:

    enter image description here

    单击加号按钮并添加新的运行时属性:

    placeholderLabel.textColor (斯威夫特4)

    _placeholderLabel.textColor (斯威夫特3或以下)

    使用 Color 作为类型并选择颜色 .

    而已!

    在您再次运行应用程序之前,您不会看到结果 .

  • 110

    像这样创建 UITextField 扩展:

    extension UITextField{
       @IBInspectable var placeHolderColor: UIColor? {
            get {
                return self.placeHolderColor
            }
            set {
                self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: newValue!])
            }
        }
    }
    

    在你的故事板或.xib中 . 你会看见

    enter image description here

  • 22

    此代码适用于Swift3:

    yourTextFieldName .setValue(UIColor.init(colorLiteralRed: 80/255, green: 80/255, blue: 80/255, alpha: 1.0), forKeyPath: "_placeholderLabel.textColor")
    

    如果您有任何问题,请告诉我 .

  • -2

    在Swift 3.0中,使用

    let color = UIColor.lightText
    textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder, attributes: [NSForegroundColorAttributeName : color])
    
  • 11

    要为应用中的所有 UITextField 设置一次占位符颜色,您可以执行以下操作:

    UILabel.appearanceWhenContainedInInstancesOfClasses([UITextField.self]).textColor = UIColor.redColor()
    

    这将为整个应用程序中的所有 TextField 占位符设置所需的颜色 . 但它仅在iOS 9之后可用 .

    在iOS 9中没有appearenceWhenContainedIn ....()方法在swift中你可以使用这里提供的解决方案之一appearanceWhenContainedIn in Swift

  • 7

    Swift 3(可能是2),您可以在 UITextField 子类中覆盖占位符上的didSet以对其应用属性,这样:

    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
        }
    }
    
  • 1

    这是我对swift 4的快速实现:

    extension UITextField {
        func placeholderColor(_ color: UIColor){
            var placeholderText = ""
            if self.placeholder != nil{
                placeholderText = self.placeholder!
            }
            self.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedStringKey.foregroundColor : color])
        }
    }
    

    使用如下:

    streetTextField?.placeholderColor(AppColor.blueColor)
    

    希望它可以帮到某人!

  • 3

    For Swift

    创建UITextField扩展

    extension UITextField{
    
        func setPlaceHolderColor(){
            self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName : UIColor.white])
        }
    }
    

    如果您是从故事板设置的 .

    extension UITextField{
        @IBInspectable var placeHolderColor: UIColor? {
            get {
                return self.placeHolderColor
            }
            set {
                self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor : newValue!])
            }
        }
    }
    
  • 3

    我很惊讶地看到这里有多少糟糕的解决方案 .

    这是一个永远有效的版本 .

    Swift 4.2

    extension UITextField{
        @IBInspectable var placeholderColor: UIColor {
            get {
                return self.attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor ?? .lightText
            }
            set {
                self.attributedPlaceholder = NSAttributedString(string: self.placeholder ?? "", attributes: [.foregroundColor: newValue])
            }
        }
    }
    

    TIP :如果在设置颜色后更改占位符文本,颜色将重置 .

  • 231

    对于斯威夫特

    func setPlaceholderColor(textField: UITextField, placeholderText: String) {
        textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor.pelorBlack])
    }
    

    你可以用这个;

    self.setPlaceholderColor(textField: self.emailTextField, placeholderText: "E-Mail/Username")
    
  • 19

    crubio's answer update for Swift 4

    Select the UITextField and open the identity inspector on the right:

    单击加号按钮并添加新的运行时属性: placeholderLabel.textColor (而不是_placeholderLabel.textColor)

    使用颜色作为类型并选择颜色 .

    如果您运行项目,您将看到更改 .

  • 0

    对于Swift 4

    txtField1.attributedPlaceholder = NSAttributedString(string: "-", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    
  • 0

    它更多的是个性化你的textField,但无论如何我将从另一个页面分享这个代码,并使它更好一点:

    import UIKit
    extension UITextField {
    func setBottomLine(borderColor: UIColor, fontColor: UIColor, placeHolderColor:UIColor, placeHolder: String) {
        self.borderStyle = UITextBorderStyle.none
        self.backgroundColor = UIColor.clear
        let borderLine = UIView()
        let height = 1.0
        borderLine.frame = CGRect(x: 0, y: Double(self.frame.height) - height, width: Double(self.frame.width), height: height)
        self.textColor = fontColor
        borderLine.backgroundColor = borderColor
        self.addSubview(borderLine)
        self.attributedPlaceholder = NSAttributedString(
            string: placeHolder,
            attributes: [NSAttributedStringKey.foregroundColor: placeHolderColor]
        )
      }
    }
    

    你可以像这样使用它:

    self.textField.setBottomLine(borderColor: lineColor, fontColor: fontColor, placeHolderColor: placeHolderColor, placeHolder: placeHolder)
    

    知道你有 UITextField 连接到 ViewController .

    资料来源:http://codepany.com/blog/swift-3-custom-uitextfield-with-single-line-input/

  • 0

    对于swift 4.2及更高版本,您可以执行以下操作:

    textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
    
  • 2

    使用此选项添加属性占位符:

    let attributes : [String : Any]  = [ NSForegroundColorAttributeName: UIColor.lightGray,
                                         NSFontAttributeName : UIFont(name: "Helvetica Neue Light Italic", size: 12.0)!
                                       ]
    x_textfield.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes:attributes)
    
  • -1
    yourTextfield.attributedPlaceholder = NSAttributedString(string: "your placeholder text",attributes: [NSForegroundColorAttributeName: UIColor.white])
    

相关问题