textviwe限制字数,包含,联想,中文高亮,emoji表情,粘贴截断的限制

分三个步骤

1.shouldChangeTextInRange处理

- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//开始计算总字数,一个字符length为1,但是表情可能为多个所以此处每多一个表情,总个数自加1
    __block NSInteger count = 0;
    @weakify(self)
    [textView.text enumerateSubstringsInRange:NSMakeRange(0, textView.text.length)
                                      options:NSStringEnumerationByComposedCharacterSequences
                                   usingBlock:^(NSString *substring,NSRange substringRange,NSRange enclosingRange,BOOL *stop){
                                       @strongify(self)
//判断是emoji表情就++,有些emoji为1有些为2有些为4,具体数字可以在此处算出length自己加,demo只是简单的把每个表情当作了1
                                       if ([self stringContainsEmoji:substring]) {
                                           count++;
                                       }
                                   }];
//maxCount就是算上表情之后的总字数了
    NSInteger maxCount = maxLength + count;
//如果是删除就允许编辑
    if ([text isEqualToString:@""]) {
        return YES;
    }
//处理高亮部分,用于中文
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    if (selectedRange && position) {
        NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];
        NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];
        NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
        
        if (offsetRange.location < maxCount) {
            return YES;
        }else{
//已超出字数限制的提示
            return NO;
        }
    }
//开始计算,如果textview的length已经满了,直接NO
    if (range.location >= maxCount) {
//已超出字数限制的提示
        return NO;
    }
//此处防止粘贴直接满的情况
    if (textView.text.length >= maxCount) {
//已超出字数限制的提示
        return YES;
    }
    return YES;
}

2.textViewDidChange

- (void)textViewDidChange:(YYTextView *)textView {
    __block NSInteger count = 0;
    @weakify(self)
    [textView.text enumerateSubstringsInRange:NSMakeRange(0, textView.text.length)
                                      options:NSStringEnumerationByComposedCharacterSequences
                                   usingBlock:^(NSString *substring,NSRange substringRange,NSRange enclosingRange,BOOL *stop){
                                       @strongify(self)
                                       if ([self stringContainsEmoji:substring]) {
                                           count++;
                                       }
                                   }];
    NSInteger maxCount = maxLength + count;
    
    if (textView.text.length >= maxCount) {
        textView.text = [textView.text substringToIndex:maxCount];
        return;
    }
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变,就不要计算字符了
    if (selectedRange && position) {
        return;
    }
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum > maxCount){
        //截取到最大位置的字符,达到最大字数限制
        NSString *s = [nsTextContent substringToIndex:maxCount];
        [textView setText:s];
    }
}

 

你可能感兴趣的:(textviwe限制字数,包含,联想,中文高亮,emoji表情,粘贴截断的限制)