首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Swift更改占位符文本颜色

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

Stack Overflow用户
提问于 2014-09-27 23:10:35
回答 20查看 217.9K关注 0票数 267

我有一个实现了深蓝色UITextField的设计,因为占位符文本默认是深灰色的,我几乎不能理解占位符文本所说的内容。

当然,我已经用谷歌搜索了这个问题,但在使用Swift语言而不是Obj-c时,我还没有想出解决方案。

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

EN

回答 20

Stack Overflow用户

发布于 2016-05-24 22:44:08

如下所示创建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中。你会看到

票数 139
EN

Stack Overflow用户

发布于 2017-02-11 01:35:31

在Swift 3.0中,使用

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

在Siwft 5.0 +中使用

let color = UIColor.lightText
let placeholder = textField.placeholder ?? "" //There should be a placeholder set in storyboard or elsewhere string or pass empty
textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : color])
票数 37
EN

Stack Overflow用户

发布于 2016-09-27 12:14:29

下面的代码可以在Swift3中运行:

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

如果你有任何问题,请告诉我。

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26076054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档