Changing Placeholder Text Color with Swift

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

    In my case, I had to make the placeholder into black color. The name of my UITextField is passwordText. Below code is tested in Swift 5 and is working fine for me. I also had an existing text for the corresponding placeholder.

    let placeholderColor = UIColor.black
    passwordText.attributedPlaceholder = NSAttributedString(string: passwordText.placeholder!, attributes: [NSAttributedString.Key.foregroundColor : placeholderColor])
    
    0 讨论(0)
  • 2020-11-29 16:22

    For Swift 3 and 3.1 this works perfectly fine:

    passField.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSForegroundColorAttributeName: UIColor.white])
    
    0 讨论(0)
  • 2020-11-29 16:24

    You can accomplish this quickly, without adding a line of code, using Interface Builder.

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

    Click on the plus button and add a new runtime attribute:

    placeholderLabel.textColor (Swift 4)

    _placeholderLabel.textColor (Swift 3 or less)

    Use Color as type and select the color.

    That's it.

    You wont see the result until you run your app again.

    0 讨论(0)
  • 2020-11-29 16:24

    To set the placeholder color once for all the UITextField in your app you can do:

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

    This will set the desired color for all TextField placeholders in the entire app. But it is only available since iOS 9.

    There is no appearenceWhenContainedIn....() method before iOS 9 in swift but you can use one of the solutions provided here appearanceWhenContainedIn in Swift

    0 讨论(0)
  • 2020-11-29 16:24

    It is more about personalize your textField but anyways I'll share this code got from another page and made it a little better:

    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]
        )
      }
    }
    

    And you can use it like this:

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

    Knowing that you have an UITextField connected to a ViewController.

    Source: http://codepany.com/blog/swift-3-custom-uitextfield-with-single-line-input/

    0 讨论(0)
  • This code is working in Swift3:

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

    let me know if you have any issue.

    0 讨论(0)
提交回复
热议问题