iOS UITextView 输入字数限制 去除虚拟键盘文本限制

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    NSString *toBeString = textView.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (toBeString.length > MaxTextViewLength) {
                textView.text = [toBeString substringToIndex:MaxTextViewLength];
            }
        }
    }
    else{// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
        if (toBeString.length > MaxTextViewLength){
            textView.text = [toBeString substringToIndex:MaxTextViewLength];
        }
    }
    self.circleDesPlaceholder.hidden = toBeString.length > 0;
    self.desNumlbl.text = [NSString stringWithFormat:@"%d/50",textView.text.length];
}

你可能感兴趣的:(iOS UITextView 输入字数限制 去除虚拟键盘文本限制)