【问题标题】:Changing Placeholder Text Color Programmatically [duplicate]以编程方式更改占位符文本颜色 [重复]
【发布时间】:2019-02-24 02:18:01
【问题描述】:

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

这是我的代码

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

提前谢谢你。

【问题讨论】:

  • 欢迎来到 Stack Overflow。这个问题已经被问过很多次了。请在询问之前至少做一些简单的搜索 - stackoverflow.com/questions/1340224/…
  • 谢谢。不幸的是,你可能有点太快去判断别人了。两个帖子都没有提供答案 SH_Khan。缺少的 KEY - NSAttributedStringKey.backgroundColor:UIColor.black.
  • @JamesC 你说你不会想从这个答案stackoverflow.com/a/26076202/1974224UIColor.white 更改为UIColor.black
  • @JamesC 如果链接的问题对您没有帮助,那么您应该通过告诉我们您卡在哪里来明确问题。现在看来,这个问题只是在 SO 上被问了 N 次的另一个问题。

标签: swift uitextfield


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    你可以试试这个(Swift 4.1)

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

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2013-05-12
      • 2020-04-06
      • 2017-05-18
      • 2013-02-04
      • 2017-11-25
      • 2010-11-23
      • 1970-01-01
      相关资源
      最近更新 更多