Clear UITextField Placeholder text on tap

后端 未结 10 1626
情书的邮戳
情书的邮戳 2021-01-31 04:33

In Xcode4 I\'ve created some placeholder text for a UITextField and I\'d like it to clear when the user taps in the box.

So, in the Attributes Inspector for the text fie

10条回答
  •  执念已碎
    2021-01-31 05:26

    If you have more than one TextField

    1) Add String variable to your class

    class YourViewController : UIViewController {
    
    var placeHolder = ""
    

    2) Add UITextFieldDelegate

    extension YourViewController : UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        placeHolder = textField.placeholder ?? ""
        textField.placeholder = ""
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField.placeholder == ""{
        textField.placeholder = placeHolder
        }
    }
    

提交回复
热议问题