Clear UITextField Placeholder text on tap












21














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 field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).



Is there any way of removing the placeholder text immediately on tapping in the text box?










share|improve this question



























    21














    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 field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).



    Is there any way of removing the placeholder text immediately on tapping in the text box?










    share|improve this question

























      21












      21








      21


      10





      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 field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).



      Is there any way of removing the placeholder text immediately on tapping in the text box?










      share|improve this question













      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 field I've clicked "Clear when editing begins" however this does not immediately remove the text when I tap in the text box (it only disappears when you start typing).



      Is there any way of removing the placeholder text immediately on tapping in the text box?







      iphone uitextfield






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 16 '11 at 10:31









      Snowcrash

      37.1k39132212




      37.1k39132212
























          10 Answers
          10






          active

          oldest

          votes


















          66














          make your ViewController the delegate of the textField and implement those two methods:



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

          - (void)textFieldDidEndEditing:(UITextField *)textField {
          textField.placeholder = @"Your Placeholdertext";
          }





          share|improve this answer





















          • In the second method you have to check that the textfield has empty value or not?
            – KingofBliss
            Dec 16 '11 at 10:43






          • 4




            nope. The placeholder text just isn't visible if there is "regular" text.
            – Matthias Bauch
            Dec 16 '11 at 10:46












          • Oh.. Yeah i forget.. :) Thanks for the info..
            – KingofBliss
            Dec 16 '11 at 10:47










          • i have add some code into my answer to check is text field is empty or not
            – Maulik
            Dec 16 '11 at 10:57








          • 2




            FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
            – zhangyoufu
            Sep 20 '13 at 10:47



















          8














          The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField to worry about? Now you have to identify which UITextField is referred to in textFieldDidEndEditing:textField (possibly by use of the tag property), and that results in more unnecessary code and logic.



          A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField doesn't have to identify the textField to set back its corresponding text after it was nullified as in Bauch's solution.



          - (void)textFieldDidBeginEditing:(UITextField *)textField {
          [textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
          }

          - (void)textFieldDidEndEditing:(UITextField *)textField {
          [textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
          }





          share|improve this answer























          • This could cause app store rejection for using private API
            – Hellojeffy
            Sep 11 '16 at 17:30



















          3














          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";
          }
          }





          share|improve this answer































            1














            use this..



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


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






            share|improve this answer





























              1














              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
              }
              }





              share|improve this answer





























                0














                In your .h file declare a function like



                -(IBAction)clear:(id)sender;



                attach this function to your touchdown event of your UITextField.



                in your .m file



                 -(IBAction)clear:(id)sender 

                {
                myplaceHolderText.text=@"";
                }





                share|improve this answer





























                  0














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


                  textfield delegate make place holder value to nil






                  share|improve this answer































                    0














                    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")
                    }
                    }





                    share|improve this answer































                      0














                      In case of SWIFT 3 or later



                      func textFieldDidBeginEditing(_ textField: UITextField) {
                      textField.placeholder = nil
                      }

                      func textFieldDidEndEditing(_ textField: UITextField) {
                      textField.placeholder = "Text Placeholder"
                      }





                      share|improve this answer





























                        -1














                        On Button action Event put this Code:



                        txtName.placeholder = @"";





                        share|improve this answer





















                          Your Answer






                          StackExchange.ifUsing("editor", function () {
                          StackExchange.using("externalEditor", function () {
                          StackExchange.using("snippets", function () {
                          StackExchange.snippets.init();
                          });
                          });
                          }, "code-snippets");

                          StackExchange.ready(function() {
                          var channelOptions = {
                          tags: "".split(" "),
                          id: "1"
                          };
                          initTagRenderer("".split(" "), "".split(" "), channelOptions);

                          StackExchange.using("externalEditor", function() {
                          // Have to fire editor after snippets, if snippets enabled
                          if (StackExchange.settings.snippets.snippetsEnabled) {
                          StackExchange.using("snippets", function() {
                          createEditor();
                          });
                          }
                          else {
                          createEditor();
                          }
                          });

                          function createEditor() {
                          StackExchange.prepareEditor({
                          heartbeatType: 'answer',
                          autoActivateHeartbeat: false,
                          convertImagesToLinks: true,
                          noModals: true,
                          showLowRepImageUploadWarning: true,
                          reputationToPostImages: 10,
                          bindNavPrevention: true,
                          postfix: "",
                          imageUploader: {
                          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                          allowUrls: true
                          },
                          onDemand: true,
                          discardSelector: ".discard-answer"
                          ,immediatelyShowMarkdownHelp:true
                          });


                          }
                          });














                          draft saved

                          draft discarded


















                          StackExchange.ready(
                          function () {
                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8532874%2fclear-uitextfield-placeholder-text-on-tap%23new-answer', 'question_page');
                          }
                          );

                          Post as a guest















                          Required, but never shown

























                          10 Answers
                          10






                          active

                          oldest

                          votes








                          10 Answers
                          10






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes









                          66














                          make your ViewController the delegate of the textField and implement those two methods:



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

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          textField.placeholder = @"Your Placeholdertext";
                          }





                          share|improve this answer





















                          • In the second method you have to check that the textfield has empty value or not?
                            – KingofBliss
                            Dec 16 '11 at 10:43






                          • 4




                            nope. The placeholder text just isn't visible if there is "regular" text.
                            – Matthias Bauch
                            Dec 16 '11 at 10:46












                          • Oh.. Yeah i forget.. :) Thanks for the info..
                            – KingofBliss
                            Dec 16 '11 at 10:47










                          • i have add some code into my answer to check is text field is empty or not
                            – Maulik
                            Dec 16 '11 at 10:57








                          • 2




                            FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
                            – zhangyoufu
                            Sep 20 '13 at 10:47
















                          66














                          make your ViewController the delegate of the textField and implement those two methods:



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

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          textField.placeholder = @"Your Placeholdertext";
                          }





                          share|improve this answer





















                          • In the second method you have to check that the textfield has empty value or not?
                            – KingofBliss
                            Dec 16 '11 at 10:43






                          • 4




                            nope. The placeholder text just isn't visible if there is "regular" text.
                            – Matthias Bauch
                            Dec 16 '11 at 10:46












                          • Oh.. Yeah i forget.. :) Thanks for the info..
                            – KingofBliss
                            Dec 16 '11 at 10:47










                          • i have add some code into my answer to check is text field is empty or not
                            – Maulik
                            Dec 16 '11 at 10:57








                          • 2




                            FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
                            – zhangyoufu
                            Sep 20 '13 at 10:47














                          66












                          66








                          66






                          make your ViewController the delegate of the textField and implement those two methods:



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

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          textField.placeholder = @"Your Placeholdertext";
                          }





                          share|improve this answer












                          make your ViewController the delegate of the textField and implement those two methods:



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

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          textField.placeholder = @"Your Placeholdertext";
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 16 '11 at 10:37









                          Matthias Bauch

                          83k15205242




                          83k15205242












                          • In the second method you have to check that the textfield has empty value or not?
                            – KingofBliss
                            Dec 16 '11 at 10:43






                          • 4




                            nope. The placeholder text just isn't visible if there is "regular" text.
                            – Matthias Bauch
                            Dec 16 '11 at 10:46












                          • Oh.. Yeah i forget.. :) Thanks for the info..
                            – KingofBliss
                            Dec 16 '11 at 10:47










                          • i have add some code into my answer to check is text field is empty or not
                            – Maulik
                            Dec 16 '11 at 10:57








                          • 2




                            FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
                            – zhangyoufu
                            Sep 20 '13 at 10:47


















                          • In the second method you have to check that the textfield has empty value or not?
                            – KingofBliss
                            Dec 16 '11 at 10:43






                          • 4




                            nope. The placeholder text just isn't visible if there is "regular" text.
                            – Matthias Bauch
                            Dec 16 '11 at 10:46












                          • Oh.. Yeah i forget.. :) Thanks for the info..
                            – KingofBliss
                            Dec 16 '11 at 10:47










                          • i have add some code into my answer to check is text field is empty or not
                            – Maulik
                            Dec 16 '11 at 10:57








                          • 2




                            FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
                            – zhangyoufu
                            Sep 20 '13 at 10:47
















                          In the second method you have to check that the textfield has empty value or not?
                          – KingofBliss
                          Dec 16 '11 at 10:43




                          In the second method you have to check that the textfield has empty value or not?
                          – KingofBliss
                          Dec 16 '11 at 10:43




                          4




                          4




                          nope. The placeholder text just isn't visible if there is "regular" text.
                          – Matthias Bauch
                          Dec 16 '11 at 10:46






                          nope. The placeholder text just isn't visible if there is "regular" text.
                          – Matthias Bauch
                          Dec 16 '11 at 10:46














                          Oh.. Yeah i forget.. :) Thanks for the info..
                          – KingofBliss
                          Dec 16 '11 at 10:47




                          Oh.. Yeah i forget.. :) Thanks for the info..
                          – KingofBliss
                          Dec 16 '11 at 10:47












                          i have add some code into my answer to check is text field is empty or not
                          – Maulik
                          Dec 16 '11 at 10:57






                          i have add some code into my answer to check is text field is empty or not
                          – Maulik
                          Dec 16 '11 at 10:57






                          2




                          2




                          FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
                          – zhangyoufu
                          Sep 20 '13 at 10:47




                          FYI, there's a bug on iOS 7.0 when you set UITextField as center-aligned, assigned a placeholder for it, then clear the placeholder (whether nil or @""). Start editing UITextField, you'll find the cursor at weird position.
                          – zhangyoufu
                          Sep 20 '13 at 10:47













                          8














                          The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField to worry about? Now you have to identify which UITextField is referred to in textFieldDidEndEditing:textField (possibly by use of the tag property), and that results in more unnecessary code and logic.



                          A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField doesn't have to identify the textField to set back its corresponding text after it was nullified as in Bauch's solution.



                          - (void)textFieldDidBeginEditing:(UITextField *)textField {
                          [textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          [textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }





                          share|improve this answer























                          • This could cause app store rejection for using private API
                            – Hellojeffy
                            Sep 11 '16 at 17:30
















                          8














                          The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField to worry about? Now you have to identify which UITextField is referred to in textFieldDidEndEditing:textField (possibly by use of the tag property), and that results in more unnecessary code and logic.



                          A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField doesn't have to identify the textField to set back its corresponding text after it was nullified as in Bauch's solution.



                          - (void)textFieldDidBeginEditing:(UITextField *)textField {
                          [textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          [textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }





                          share|improve this answer























                          • This could cause app store rejection for using private API
                            – Hellojeffy
                            Sep 11 '16 at 17:30














                          8












                          8








                          8






                          The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField to worry about? Now you have to identify which UITextField is referred to in textFieldDidEndEditing:textField (possibly by use of the tag property), and that results in more unnecessary code and logic.



                          A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField doesn't have to identify the textField to set back its corresponding text after it was nullified as in Bauch's solution.



                          - (void)textFieldDidBeginEditing:(UITextField *)textField {
                          [textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          [textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }





                          share|improve this answer














                          The solution provided by Matthias Bauch works well, but what happens when you have more than one UITextField to worry about? Now you have to identify which UITextField is referred to in textFieldDidEndEditing:textField (possibly by use of the tag property), and that results in more unnecessary code and logic.



                          A much simpler solution: simply assign a clear color to the placeholder text , and when done editing, revert back to it's original color. This way, your textFieldDidEndEditing:textField doesn't have to identify the textField to set back its corresponding text after it was nullified as in Bauch's solution.



                          - (void)textFieldDidBeginEditing:(UITextField *)textField {
                          [textField setValue:[UIColor clearColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }

                          - (void)textFieldDidEndEditing:(UITextField *)textField {
                          [textField setValue:[UIColor placeholderColor] forKeyPath:@"_placeholderLabel.textColor"];
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 23 '16 at 10:58









                          Meet Doshi

                          2,65782766




                          2,65782766










                          answered Feb 6 '14 at 9:04









                          etayluz

                          7,58265584




                          7,58265584












                          • This could cause app store rejection for using private API
                            – Hellojeffy
                            Sep 11 '16 at 17:30


















                          • This could cause app store rejection for using private API
                            – Hellojeffy
                            Sep 11 '16 at 17:30
















                          This could cause app store rejection for using private API
                          – Hellojeffy
                          Sep 11 '16 at 17:30




                          This could cause app store rejection for using private API
                          – Hellojeffy
                          Sep 11 '16 at 17:30











                          3














                          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";
                          }
                          }





                          share|improve this answer




























                            3














                            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";
                            }
                            }





                            share|improve this answer


























                              3












                              3








                              3






                              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";
                              }
                              }





                              share|improve this answer














                              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";
                              }
                              }






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 20 '11 at 4:26

























                              answered Dec 16 '11 at 10:57









                              Maulik

                              18.1k1473130




                              18.1k1473130























                                  1














                                  use this..



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


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






                                  share|improve this answer


























                                    1














                                    use this..



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


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






                                    share|improve this answer
























                                      1












                                      1








                                      1






                                      use this..



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


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






                                      share|improve this answer












                                      use this..



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


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







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 16 '11 at 10:36









                                      Ankit Srivastava

                                      9,754650104




                                      9,754650104























                                          1














                                          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
                                          }
                                          }





                                          share|improve this answer


























                                            1














                                            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
                                            }
                                            }





                                            share|improve this answer
























                                              1












                                              1








                                              1






                                              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
                                              }
                                              }





                                              share|improve this answer












                                              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
                                              }
                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 12 at 9:59









                                              Ammar

                                              1828




                                              1828























                                                  0














                                                  In your .h file declare a function like



                                                  -(IBAction)clear:(id)sender;



                                                  attach this function to your touchdown event of your UITextField.



                                                  in your .m file



                                                   -(IBAction)clear:(id)sender 

                                                  {
                                                  myplaceHolderText.text=@"";
                                                  }





                                                  share|improve this answer


























                                                    0














                                                    In your .h file declare a function like



                                                    -(IBAction)clear:(id)sender;



                                                    attach this function to your touchdown event of your UITextField.



                                                    in your .m file



                                                     -(IBAction)clear:(id)sender 

                                                    {
                                                    myplaceHolderText.text=@"";
                                                    }





                                                    share|improve this answer
























                                                      0












                                                      0








                                                      0






                                                      In your .h file declare a function like



                                                      -(IBAction)clear:(id)sender;



                                                      attach this function to your touchdown event of your UITextField.



                                                      in your .m file



                                                       -(IBAction)clear:(id)sender 

                                                      {
                                                      myplaceHolderText.text=@"";
                                                      }





                                                      share|improve this answer












                                                      In your .h file declare a function like



                                                      -(IBAction)clear:(id)sender;



                                                      attach this function to your touchdown event of your UITextField.



                                                      in your .m file



                                                       -(IBAction)clear:(id)sender 

                                                      {
                                                      myplaceHolderText.text=@"";
                                                      }






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Dec 16 '11 at 10:48









                                                      Ajeet Pratap Maurya

                                                      3,64432443




                                                      3,64432443























                                                          0














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


                                                          textfield delegate make place holder value to nil






                                                          share|improve this answer




























                                                            0














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


                                                            textfield delegate make place holder value to nil






                                                            share|improve this answer


























                                                              0












                                                              0








                                                              0






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


                                                              textfield delegate make place holder value to nil






                                                              share|improve this answer














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


                                                              textfield delegate make place holder value to nil







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Apr 8 '16 at 11:37









                                                              Maciek z Wrocławia

                                                              4,69213040




                                                              4,69213040










                                                              answered Aug 6 '14 at 6:04









                                                              chaithraVeeresh

                                                              227310




                                                              227310























                                                                  0














                                                                  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")
                                                                  }
                                                                  }





                                                                  share|improve this answer




























                                                                    0














                                                                    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")
                                                                    }
                                                                    }





                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0






                                                                      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")
                                                                      }
                                                                      }





                                                                      share|improve this answer














                                                                      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")
                                                                      }
                                                                      }






                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Jul 12 '16 at 11:40

























                                                                      answered Jul 12 '16 at 10:54









                                                                      Yurii Koval

                                                                      219314




                                                                      219314























                                                                          0














                                                                          In case of SWIFT 3 or later



                                                                          func textFieldDidBeginEditing(_ textField: UITextField) {
                                                                          textField.placeholder = nil
                                                                          }

                                                                          func textFieldDidEndEditing(_ textField: UITextField) {
                                                                          textField.placeholder = "Text Placeholder"
                                                                          }





                                                                          share|improve this answer


























                                                                            0














                                                                            In case of SWIFT 3 or later



                                                                            func textFieldDidBeginEditing(_ textField: UITextField) {
                                                                            textField.placeholder = nil
                                                                            }

                                                                            func textFieldDidEndEditing(_ textField: UITextField) {
                                                                            textField.placeholder = "Text Placeholder"
                                                                            }





                                                                            share|improve this answer
























                                                                              0












                                                                              0








                                                                              0






                                                                              In case of SWIFT 3 or later



                                                                              func textFieldDidBeginEditing(_ textField: UITextField) {
                                                                              textField.placeholder = nil
                                                                              }

                                                                              func textFieldDidEndEditing(_ textField: UITextField) {
                                                                              textField.placeholder = "Text Placeholder"
                                                                              }





                                                                              share|improve this answer












                                                                              In case of SWIFT 3 or later



                                                                              func textFieldDidBeginEditing(_ textField: UITextField) {
                                                                              textField.placeholder = nil
                                                                              }

                                                                              func textFieldDidEndEditing(_ textField: UITextField) {
                                                                              textField.placeholder = "Text Placeholder"
                                                                              }






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Oct 31 '17 at 10:34









                                                                              Ghulam Rasool

                                                                              2,09911326




                                                                              2,09911326























                                                                                  -1














                                                                                  On Button action Event put this Code:



                                                                                  txtName.placeholder = @"";





                                                                                  share|improve this answer


























                                                                                    -1














                                                                                    On Button action Event put this Code:



                                                                                    txtName.placeholder = @"";





                                                                                    share|improve this answer
























                                                                                      -1












                                                                                      -1








                                                                                      -1






                                                                                      On Button action Event put this Code:



                                                                                      txtName.placeholder = @"";





                                                                                      share|improve this answer












                                                                                      On Button action Event put this Code:



                                                                                      txtName.placeholder = @"";






                                                                                      share|improve this answer












                                                                                      share|improve this answer



                                                                                      share|improve this answer










                                                                                      answered Dec 16 '11 at 10:37









                                                                                      Developer

                                                                                      457316




                                                                                      457316






























                                                                                          draft saved

                                                                                          draft discarded




















































                                                                                          Thanks for contributing an answer to Stack Overflow!


                                                                                          • Please be sure to answer the question. Provide details and share your research!

                                                                                          But avoid



                                                                                          • Asking for help, clarification, or responding to other answers.

                                                                                          • Making statements based on opinion; back them up with references or personal experience.


                                                                                          To learn more, see our tips on writing great answers.





                                                                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                          Please pay close attention to the following guidance:


                                                                                          • Please be sure to answer the question. Provide details and share your research!

                                                                                          But avoid



                                                                                          • Asking for help, clarification, or responding to other answers.

                                                                                          • Making statements based on opinion; back them up with references or personal experience.


                                                                                          To learn more, see our tips on writing great answers.




                                                                                          draft saved


                                                                                          draft discarded














                                                                                          StackExchange.ready(
                                                                                          function () {
                                                                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8532874%2fclear-uitextfield-placeholder-text-on-tap%23new-answer', 'question_page');
                                                                                          }
                                                                                          );

                                                                                          Post as a guest















                                                                                          Required, but never shown





















































                                                                                          Required, but never shown














                                                                                          Required, but never shown












                                                                                          Required, but never shown







                                                                                          Required, but never shown

































                                                                                          Required, but never shown














                                                                                          Required, but never shown












                                                                                          Required, but never shown







                                                                                          Required, but never shown







                                                                                          Popular posts from this blog

                                                                                          Florida Star v. B. J. F.

                                                                                          Error while running script in elastic search , gateway timeout

                                                                                          Adding quotations to stringified JSON object values