UITextView 字数限制


- (void)textViewDidChange:(UITextView *)textView {
    NSString *lang = [[textView textInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
        if (!position) {
            if (textView.text.length > self.maxNumber) {
                textView.text = [textView.text substringToIndex:self.maxNumber];
            }
            if (self.inputTextCount) {
                self.inputTextCount(textView.text.length);
            }
        } else {
            // 有高亮选择的字符串,则暂不对文字进行统计和限制
        }
    } else { // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
        if (textView.text.length > self.maxNumber) {
            textView.text = [textView.text substringToIndex:self.maxNumber];
        }
        if (self.inputTextCount) {
            self.inputTextCount(textView.text.length);
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([self isText:textView beyondLimit:self.maxNumber] && [text length] > 0) {
        return NO;
    }
    return YES;
}

- (BOOL)isText:(UITextView *)tv beyondLimit:(int)maxLimit{
    NSString *lang = [[tv textInputMode] primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"]) {
        UITextRange *selectedRange = [tv markedTextRange];
        UITextPosition *position = [tv positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            if (tv.text.length > maxLimit) {
                return YES;
            }
        }
    } else {
        if (tv.text.length > maxLimit) {
            return YES;
        }
    }
    
    return NO;
}



行数限制

- (void)textViewDidChange:(UITextView *)textView {
    if (self.numberOfLines != NSIntegerMax && !_isDelete) {
        float limitHeight = self.font.lineHeight * self.numberOfLines ;
        if ([self getTextContentSize].height > limitHeight) {
            while ([self getTextContentSize].height > limitHeight) {
                self.text = [self.text substringToIndex:self.text.length - 1];
            }
        }
    }
}

- (CGSize)getTextContentSize {
    return [self getStringPlaceSize:self.text textFont:self.font bundingSize:CGSizeMake(self.contentSize.width-10, CGFLOAT_MAX)];
}
- (CGSize)getStringPlaceSize:(NSString *)string textFont:(UIFont *)font bundingSize:(CGSize)boundSize {
    //计算文本高度
    NSDictionary *attribute = @{NSFontAttributeName:font};
    NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);
    CGSize size = [string boundingRectWithSize:boundSize options:option attributes:attribute context:nil].size;
    return size;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([self isText:textView beyondLimit:self.maxNumber] && [text length] > 0) {
        return NO;
    }
    self.isDelete = text.length == 0;
    return YES;
}



你可能感兴趣的:(UITextView 字数限制)