设为首页 加入收藏

TOP

iOS 输入框限制输入字节数(一)
2019-08-31 01:04:12 】 浏览:32
Tags: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记录
iOS 输入框限制输入字节数(一) https://www.cppentry.com/bencandy.php?fid=99&id=233637

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS根据图片url获取尺寸 下一篇使用Hbuilder将自己app发布到App ..