如果我在XIB中设计了textField,如何以编程方式更改UItextField的placeHolder文本

时间:2019-03-25 09:29:43

标签: ios swift swift4

我必须创建一个自定义类以在textField中设计XIB,然后在我的viewController中多次重用它。我想以编程方式更改每个占位符名称。

6 个答案:

答案 0 :(得分:0)

在文本字段自定义类中声明一个字符串类型的打开变量。访问该变量,以在任何已实现的地方更改占位符名称。

答案 1 :(得分:0)

您可以通过以下两种方式进行更改

1)yourTextField.placeholder = "some string"

2)yourTextField.attributedPlaceholder = NSAttributedString(string: "some string")

答案 2 :(得分:0)

如果要更改占位符,可以使用以下代码

yourTextField.attributedPlaceholder =
            NSAttributedString(string: "YourText", attributes: [NSForegroundColorAttributeName : UIColor.white])

答案 3 :(得分:0)

尝试创建自定义uitextfield类

@IBDesignable
class CustomTextField: UITextField {

    @IBInspectable var TFPlaceholder: String? {
        willSet {
            self.placeholder = newValue
        }
    }
}

enter image description here

祝你好运!! :)

答案 4 :(得分:0)

创建子类为:

class CustomTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    func placeHolderColor(pColor: UIColor, pText: String) {

        self.attributedPlaceholder = NSAttributedString(string: pText,
                                                               attributes: [NSAttributedStringKey.foregroundColor: pColor])

    }
}

答案 5 :(得分:0)

快速> 5.0

 class CustomTextField: UITextField {

override func awakeFromNib() {
    super.awakeFromNib()

}

func placeHolderColor(pColor: UIColor, pText: String) {
          self.attributedPlaceholder = NSAttributedString(pText,
                attributes: [NSAttributedString.Key.foregroundColor: pColor])
   }
}