以编程方式更改占位符文本颜色

Changing Placeholder Text Color Programmatically

我正在尝试将文本字段中的占位符文本更改为白色,背景颜色为黑色。以编程方式

这是我的代码

let emailTextField: UITextField = {
    let emailTF = UITextField()
    emailTF.attributedPlaceholder = NSAttributedString(string: "Enter Email", attributes: [kCTForegroundColorAttributeName as NSAttributedStringKey: kCISamplerWrapBlack])

    emailTF.translatesAutoresizingMaskIntoConstraints = false
    //User input text white
    emailTF.textColor = UIColor.white
    emailTF.backgroundColor = UIColor.black
    emailTF.borderStyle = .roundedRect
    emailTF.font = UIFont.systemFont(ofSize: 16)
    return emailTF
}()

提前致谢。

你可以试试这个 (Swift 4.1)

emailTF.attributedPlaceholder = NSAttributedString(string:"Enter Email", attributes: [NSAttributedStringKey.foregroundColor:UIColor.white,NSAttributedStringKey.backgroundColor:UIColor.black])

试试这个:

let emailTextField: UITextField = {
        let emailTF = UITextField()
        emailTF.attributedPlaceholder = NSAttributedString(string: "Enter Email",
                                                            attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

        emailTF.translatesAutoresizingMaskIntoConstraints = false
        //User input text white
        emailTF.textColor = UIColor.white
        emailTF.backgroundColor = UIColor.black
        emailTF.borderStyle = .roundedRect
        emailTF.font = UIFont.systemFont(ofSize: 16)
        return emailTF
  }()