Changing Placeholder Text Color with Swift

前端 未结 30 2333
独厮守ぢ
独厮守ぢ 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
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:07

    For Objective C:

    UIColor *color = [UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1.0];
     emailTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Friend's Email" attributes:@{NSForegroundColorAttributeName: color}];
    

    For Swift:

    emailTextField.attributedPlaceholder = NSAttributedString(string: "Friend's Email",
                                 attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
    
    0 讨论(0)
  • 2020-11-29 16:07

    Use this for adding an attributed placeholder:

    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)
    
    0 讨论(0)
  • 2020-11-29 16:08

    For Swift 4

    txtField1.attributedPlaceholder = NSAttributedString(string: "-", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    
    0 讨论(0)
  • 2020-11-29 16:09

    I'm surprised to see how many poor solutions there are here.

    Here is a version that will always work.

    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: If you change the placeholder text after setting the color- the color will reset.

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

    For Swift 4.0, X-code 9.1 version or iOS 11 you can use following syntax to have different placeholder color

    textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
    
    0 讨论(0)
提交回复
热议问题