Snowcrash
Snowcrash

Reputation: 86367

Clear UITextField Placeholder text on tap

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?

Upvotes: 22

Views: 43005

Answers (10)

Ammar
Ammar

Reputation: 380

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

Upvotes: 1

Ghulam Rasool
Ghulam Rasool

Reputation: 4064

In case of SWIFT 3 or later

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

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

Upvotes: 0

etayluz
etayluz

Reputation: 16426

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

Upvotes: 9

Yurii Koval
Yurii Koval

Reputation: 429

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

Upvotes: 0

chaithraVeeresh
chaithraVeeresh

Reputation: 258

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

textfield delegate make place holder value to nil

Upvotes: 0

Maulik
Maulik

Reputation: 19418

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

Upvotes: 3

Ajeet Pratap Maurya
Ajeet Pratap Maurya

Reputation: 4254

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

Upvotes: 0

Nishith Shah
Nishith Shah

Reputation: 523

On Button action Event put this Code:

txtName.placeholder = @"";

Upvotes: -1

Matthias Bauch
Matthias Bauch

Reputation: 90127

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

Upvotes: 68

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

use this..

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

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

Upvotes: 1

Related Questions