Clear UITextField Placeholder text on tap

后端 未结 10 1606
情书的邮戳
情书的邮戳 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
        }
    }
    
    0 讨论(0)
  • 2021-01-31 05:27

    You should also check if text filed is empty then you should put place holder again

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        textField.placeholder = nil;
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField 
    {
       if ([textField.text isEqualToString:@""] || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
       {
           [textField setText:@""];
           textField.placeholder = @"Your Placeholdertext";
       }
    }
    
    0 讨论(0)
  • 2021-01-31 05:32

    use this..

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        textField.placeholder=nil;
    }
    

    don't forget to add the delegate for the textfield to your file Owner.

    0 讨论(0)
  • 2021-01-31 05:34

    The @etayluz 's solution is better (my opinion), because you don't need to worry about assigning placeholder'a text again. If you have custom textFields in different places of your app and want them to behave equally (as I need in my case) you can add this code to your custom TextField's class:

    class CustomTextField: UITextField, UITextFieldDelegate {
         private func setup() {
        //do additional setup like attributedPlaceholder, inset, etc.
            self.delegate = self
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
            setup()
        }
    
    //    MARK: UITextFieldDelegate methods
    
        func textFieldDidBeginEditing(textField: UITextField) {
            textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
        }
    
        func textFieldDidEndEditing(textField: UITextField) {
            textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
        }
    }
    

    But if you need to have specific UITextFieldDelegate's methods for individual textField you DO need to implement this logic for it individually:

    class LoginViewController: UIViewController, UITextFieldDelegate {
    
        @IBOutlet weak var emailTextField: CustomTextField!
        @IBOutlet weak var passwordTextField: CustomTextField!
    
    override func viewDidLoad() {
            super.viewDidLoad()
    textFields = [emailTextField, passwordTextField]
            for textField in textFields {
                textField.delegate = self
            }
    
    //    MARK: UITextFieldDelegate methods
    
        func textFieldDidBeginEditing(textField: UITextField) {
            textField.setValue(UIColor.clearColor(), forKeyPath: "_placeholderLabel.textColor")
        }
    
        func textFieldDidEndEditing(textField: UITextField) {
            textField.setValue(UIColor.lightGrayColor(), forKeyPath: "_placeholderLabel.textColor")
        }
    }
    
    0 讨论(0)
提交回复
热议问题