Limit the number of input words in a UITextField

Limit the number of input words in a UITextField

To limit the number of input words of a UITextField, the first thing that comes to mind should be through UITextFieldDelegate

The proxy method to limit:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

For example, to set the word limit to 20:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.titleField) {
        if (textField.length > 20) return NO;
    }

    return YES;
}

However, such restrictions are simple and crude, and may affect the user's input under normal logic. For example, after inputting 20 characters, backspace to delete characters.
At this time, we may consider "Detect backspace in UITextField", for example, simply determine whether the length of the replacementString is 0.
Then we may encounter that the user has entered 20 characters. At this time, continue to enter-but select part of the text to replace-can not be carried out, which also hinders the user's normal operation, so the limited code version may be Evolution to:

pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.titleField) {
        if (string.length == 0) return YES;

        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 20) {
            return NO;
        }
    }

    return YES;
}

At this point, you may feel that you are basically done, but when you enter 19 characters, the 20th character continues to be entered in the form of Chinese characters, then the system will provide a series of subsequent association words at the top of the keyboard, and you will find that through this The way can continuously select words and input to break the 20-character limit. WTF

At this point, we may wish to have a similar

  • (void)textFieldDidChange:(UITextField *)textField

The callback method, but unfortunately not.
Of course, we can also pass

  • (void)textFieldDidEndEditing:(UITextField *)textField;

The callback method cuts off the text at the end of editing, although it will be a bit abrupt in the user experience.
However, when we click into the UITextField.h header file to search for the above callback method and are not satisfied, we may find that there is such a message notification name at the bottom:

UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;

However, when you listen to messages, you must remember to remove the monitoring. Usually, I am still used to putting the code for monitoring messages in one method, which seems to be a bit of a big move.
Fortunately, UITextField itself provides the corresponding
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
这里写代码片to limit the input length:

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.titleField) {
        if (textField.text.length > 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

Guess you like

Origin blog.csdn.net/woruosuifenglang/article/details/53928468