Question

Is there any way to prevent placeholder text in a UITextField from being overwritten when the text field receives an input? I have a text field that takes an expiration date in this format "MM/YYYY" with placeholder text that reads "MM/YYYY" and I want to remove the placeholder text as it is filled in, not when the first character is entered.

Example: "1M/YYYY" after first character "10/YYYY" after the second character etc.

Was it helpful?

Solution

Instead of using the placeholder text, you'll want to set the actual text property of the field to MM/YYYY, set the delegate of the text field and listen for this method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // update the text of the label
}

Inside that method, you can figure out what the user has typed as they type, which will allow you to update the label accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top