iOS-textView字数限制可删除,高亮联想文字处理

UItextView的代理方法在文本输入的过程中实时监测 [限制字数,可删除]

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if (range.length == 1 && text.length == 0) {
        // 删除按钮处理[不加键盘限制输入时删除键也不能用]
        return YES;
    }else if (textView.text.length > 2000 ) {
        // 键盘输入空格和删除键值的时候不用提示超限
        if ( ![text isEqualToString:@"\n"] || ![text isEqualToString:@""]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:CDNotificationMessageAppear object:self userInfo:@{@"message":[CDCommon localizedString:@"commentExceedLimitPrompt"],@"messageType":@(CDMessageTypeOnlyWords)}];
        }
        return NO;
    }
    return YES;
}

给uitextView添加监听

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewHighlightText) name:UITextViewTextDidChangeNotification object:nil];

监听方法实现 [高亮,联想文字处理]

// 监听textView
- (void)textViewHighlightText{
    for (UIView *subView in _textBgView.subviews) {
        if ([subView isKindOfClass:[UITextView class]]) {
            UITextView *textView = (UITextView *)subView;
            //中文
            if ([BFLCommon systemLanguageIsCn]) {
                UITextRange *selectedRange = [textView markedTextRange];
                UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
                // 没有高亮选择的字,则对已输入的文字进行字数统计和限制,点击联想文字不再输入
                if (!position) {
                    if (textView.text.length > 20) {
                        textView.text = [textView.text substringToIndex:20];
                      // 添加超出最大字符数提示
                        [[NSNotificationCenter defaultCenter] postNotificationName:CDNotificationMessageAppear object:self userInfo:@{@"message":[CDCommon localizedString:@"commentExceedLimitPrompt"],@"messageType":@(CDMessageTypeOnlyWords)}];
                    }
                }else{
                  // 高亮文本处理            
  
                }
            }else{
                if (textView.text.length > 20) {
                    textView.text = [textView.text substringToIndex:20];
                }
            }
            return;
        }
    }
}

系统语言判断

+ (BOOL)systemLanguageIsCn {
    NSArray *languageArray = [NSLocale preferredLanguages];
    if ([languageArray count] > 0) {
        NSString *systemLanguageKey = languageArray[0];
        if ([systemLanguageKey isKindOfClass:[NSString class]]) {
            NSRange zhRange = [systemLanguageKey rangeOfString:@"zh-Hans"];
            if (zhRange.location == NSNotFound) {
                return NO;
            } else {
                return YES;
            }
        }
    }
    return YES;
}

键盘输入模式

NSString *lang = [[UITextInputMode currentInputMode]primaryLanguage];//键盘输入模式
 if ([langisEqualToString:@"zh-Hans"]) {
          // 简体中文输入,包括简体拼音,健体五笔,简体手写
}

(currentInputMode貌似不能用了,可以通过添加监听来实现,监听键盘语言通知名:UITextInputCurrentInputModeDidChangeNotification)
移除通知

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

你可能感兴趣的:(iOS-textView字数限制可删除,高亮联想文字处理)