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

Ios 如果我在XIB中设计了textField,如何以编程方式更改UItextField的占位符文本,ios,swift,swift4,Ios,Swift,Swift4,我必须创建一个自定义类来设计XIB中的textField,并在我的viewController中多次重用它。我想以编程方式更改每个占位符名称 在textfield自定义类中声明string类型的打开变量。访问该变量以更改占位符名称(如果已实现)。您可以通过以下两种方式进行更改 1) yourTextField.placeholder=“一些字符串” 2) yourTextField.attributedPlaceholder=NSAttributedString(字符串:“某些字符串”)如果要更

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

在textfield自定义类中声明string类型的打开变量。访问该变量以更改占位符名称(如果已实现)。

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

1)
yourTextField.placeholder=“一些字符串”


2)
yourTextField.attributedPlaceholder=NSAttributedString(字符串:“某些字符串”)

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

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

尝试创建自定义uitextfield类

@IBDesignable
class CustomTextField: UITextField {

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

祝你好运!!:)

将子类创建为:

class CustomTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

    }

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

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

    }
}
对于swift>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])
   }
}

提供您需要的代码tried@IBInspectablevar placeholder text:String=“{didSet{rsFloatingView.placeHolderStringKey=placeholder text}}的可能重复项