iOS 输入框限制输入字节数

iOS中限制输入框文字长度的方法有好多,百度一下,最常用的是这种:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:self.nameTextField]) {
        NSString *whole = [NSString stringWithFormat:@"%@%@",textField.text, string];
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        if ([whole dataUsingEncoding:NSUTF8StringEncoding].length > 14) {
            [textField resignFirstResponder];
            MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
            return NO;
        }
    }
    return YES;
}

但是这种方法有bug,当输入最后一个汉字的时候,只能输入拼音不能变成汉字。

经过仔细的搜索之后会发现另一中方法:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:self.nameTextField];
- (void)textFieldDidChange:(NSNotification *)noti {
    NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
    NSString *string = self.nameTextField.text;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if ([language rangeOfString:@"zh-Hans"].length != 0 || [language rangeOfString:@"zh-Hant"].length != 0) {
        UITextRange *selectedRange = self.nameTextField.markedTextRange;
        UITextPosition *position = [self.nameTextField positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (data.length > 14) {
                self.nameTextField.text = [string substringToIndex:14];
                [self.nameTextField resignFirstResponder];
                MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
            }
        } else {

        }
    } else {
        if ([self.nameTextField.text dataUsingEncoding:NSUTF8StringEncoding].length > 14) {
            [self.nameTextField resignFirstResponder];
            MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
        }
    }
}

这种方法没有前面说的那种bug,会截取指定长度字符串,但是如果是中英文混合输入的话就没办法根据字节数计算对应的字符串长度,我们希望超过指定字节数时,截取到最后一次输入之前的长度。

可以将上面两种方法结合起来来实现我们的需求:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.length != 0) { self.lastInputStringRange = range; }
    return YES;
}
- (void)textFieldDidChange:(NSNotification *)noti {
    NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
    NSString *string = self.nameTextField.text;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if ([language rangeOfString:@"zh-Hans"].length != 0 || [language rangeOfString:@"zh-Hant"].length != 0) {
        UITextRange *selectedRange = self.nameTextField.markedTextRange;
        UITextPosition *position = [self.nameTextField positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (data.length > 14) {
                self.nameTextField.text = [string substringToIndex:self.lastInputStringRange.location];
                [self.nameTextField resignFirstResponder];
                MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
            }
        } else {

        }
    } else {
        if ([self.nameTextField.text dataUsingEncoding:NSUTF8StringEncoding].length > 14) {
            self.nameTextField.text = [string substringToIndex:14];
            [self.nameTextField resignFirstResponder];
            MAKE_TOAST_TOP(LOCALIZESTRING(@"NameLimit"));
        }
    }
}

 

使用变量lastInputStringRange记录最近一次输入的范围,超过指定范围截取字符串时,截取到该位置即可。

posted @ 2018-10-19 14:36  yuyuyu37  阅读(830)  评论(0编辑  收藏  举报