[英]How to change placeHolder text of UItextField programmatically if I have designed textField in XIB
我必須創建一個自定義類來設計XIB
textField
並在我的viewController
多次重用它。 我想以編程方式更改每個占位符名稱。
創建子類為:
class CustomTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
}
func placeHolderColor(pColor: UIColor, pText: String) {
self.attributedPlaceholder = NSAttributedString(string: pText,
attributes: [NSAttributedStringKey.foregroundColor: pColor])
}
}
在您的文本字段自定義類中聲明一個字符串類型的開放變量。 訪問該變量以更改曾經實施的占位符名稱。
您可以通過以下兩種方式進行更改
1) yourTextField.placeholder = "some string"
2) yourTextField.attributedPlaceholder = NSAttributedString(string: "some string")
如果要更改占位符,可以使用以下代碼
yourTextField.attributedPlaceholder =
NSAttributedString(string: "YourText", attributes: [NSForegroundColorAttributeName : UIColor.white])
快速> 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])
}
}
yourTextfield.attributedPlaceholder = NSAttributedString(string: "yourPlaceholderText")
這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.