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

Ale*_*ole 208 uikit ios swift

我有一个实现深蓝色的设计UITextField,因为占位符文本默认为深灰色,我几乎无法确定占位符文本所说的内容.

我当然用Google搜索了问题,但我还没有提出解决方案,而使用Swift语言而不是Obj-c.

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

vac*_*ama 459

您可以使用属性字符串设置占位符文本.通过以下方式传递您想要的颜色attributes:

var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                             attributes: [NSForegroundColorAttributeName: UIColor.yellow])
Run Code Online (Sandbox Code Playgroud)

对于Swift 3+使用以下:

myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                             attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
Run Code Online (Sandbox Code Playgroud)

对于Swift 4.2使用以下:

myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                             attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
Run Code Online (Sandbox Code Playgroud)

  • @vacawama.谢谢你的交流.遗留项目,从App Store更新了我的Xcode - 版本9.0(9A235),最后提出了self.passwordTextField的解决方案?.attributedPlaceholder = NSAttributedString(string:"PASSWORD",属性:[NSForegroundColorAttributeName:UIColor.white]) (2认同)
  • 对于iOS 11 SDK使用`myTextField.attributedPlaceholder = NSAttributedString(string:"placeholder text",属性:[NSForegroundColorAttributeName:UIColor.white])` (2认同)
  • 有没有办法在不设置字符串的情况下设置颜色? (2认同)

cru*_*bio 267

您可以使用Interface Builder快速完成此操作,而无需添加任何代码.

选择UITextField并打开右侧的身份检查器:

在此输入图像描述

单击加号按钮并添加新的运行时属性:

placeholderLabel.textColor(Swift 4)

_placeholderLabel.textColor(Swift 3或更低版本)

使用颜色作为类型并选择颜色.

而已!

在您再次运行应用程序之前,您不会看到结果.

  • 是的,但这是一种黑客行为,因为 placeholderLabel.textColor 属性是私有的...... (3认同)
  • 不幸的是,这似乎至少在iOS 10上不起作用. (2认同)
  • @nickdnk不对.现在我已经在iOS 10.2.1上测试了它的工作原理.:) (2认同)
  • 如果苹果将改变内部实施方式,则通往意外崩溃的良好之路。) (2认同)

jos*_*405 122

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!])
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的故事板或.xib中.你会看见

在此输入图像描述

  • ⚠️**警告:**如果您决定_read_属性`placeHolderColor`,此扩展程序将**崩溃**您的程序!如果从属性的getter返回计算属性本身的值,则将从getter中再次调用getter,从而产生无限递归.(见:http://stackoverflow.com/a/42334711/2062785) (10认同)

Tej*_*der 24

此代码适用于Swift3:

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

如果您有任何问题,请告诉我.


Sre*_*M S 24

在Swift 3.0中,使用

let color = UIColor.lightText
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder, attributes: [NSForegroundColorAttributeName : color])
Run Code Online (Sandbox Code Playgroud)


小智 14

要为UITextField应用中的所有内容设置一次占位符颜色,您可以执行以下操作:

UILabel.appearanceWhenContainedInInstancesOfClasses([UITextField.self]).textColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

这将为TextField整个应用程序中的所有占位符设置所需的颜色.但它仅在iOS 9之后可用.

在iOS 9中没有appearenceWhenContainedIn ....()方法在swift中但是你可以使用这里提供的一个解决方案在Swift中使用appearanceWhenContainedIn


Dix*_*ari 6

对于斯威夫特

创建 UITextField 扩展

extension UITextField{

    func setPlaceHolderColor(){
        self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName : UIColor.white])
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您是从故事板设置的。

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!])
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Kha*_*lam 6

Xcode 9.2 Swift 4

extension UITextField{
    @IBInspectable var placeHolderColor: UIColor? {
        get {
            return self.placeHolderColor
        }
        set {
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: newValue!])
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不行.getter将导致无限递归. (2认同)
  • 这在swift 4中工作得很好.我在代码中创建了断点并检查了它.没有发生无限递归. (2认同)

Álv*_*ero 5

斯威夫特 4:

txtControl.attributedPlaceholder = NSAttributedString(string: "Placeholder String...",attributes: [NSAttributedStringKey.foregroundColor: UIColor.gray])
Run Code Online (Sandbox Code Playgroud)

目标-C:

UIColor *color = [UIColor grayColor];
txtControl.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder String..." attributes:@{NSForegroundColorAttributeName: color}];
Run Code Online (Sandbox Code Playgroud)


小智 5

就我而言,我使用Swift 4

我为UITextField创建扩展

extension UITextField {
    func placeholderColor(color: UIColor) {
        let attributeString = [
            NSAttributedStringKey.foregroundColor: color.withAlphaComponent(0.6),
            NSAttributedStringKey.font: self.font!
            ] as [NSAttributedStringKey : Any]
        self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: attributeString)
    }
}
Run Code Online (Sandbox Code Playgroud)

yourField.placeholderColor(color:UIColor.white)