iOS _UITextView限制输入字符数量与行间距设置

#pragma mark -- UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.placeHolderLabel.text = @"";
}

- (void)textViewDidChange:(UITextView *)textView{
    // 判断是否有候选字符
    if (textView.markedTextRange == nil) {
        if (textView.text.length > totalLength){
            textView.text = [textView.text substringToIndex:totalLength];
            
        }
        textView.attributedText = [NSString stringWithText:textView.text textColor:k_Color_BlackColor font:kAdaptedFontSize(13) lineSpace:5 firstLineHeadIndent:0 alignment:NSTextAlignmentLeft];
    }

    self.totalLabel.text = [NSString stringWithFormat:@"%lu/%ld", (unsigned long)[textView.text length], (long)totalLength];
    
    [self rd_userInteractionEnabled];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > totalLength){
        NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:totalLength];
        if (rangeIndex.length == 1){
            textView.text = [str substringToIndex:totalLength];
            // 计数的label
            self.totalLabel.text = [NSString stringWithFormat:@"%lu/%ld", (unsigned long)textView.text.length, (long)totalLength];
            [RDWindowHudService showViewWithInfo:@"字数不得超过500"];
        }else{
            NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, totalLength)];
            textView.text = [str substringWithRange:rangeRange];
        }
        return NO;
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if (!textView.text.length) {
        self.placeHolderLabel.attributedText = [NSString stringWithText:placeHolderString
                                                              textColor:k_Color_999999
                                                                   font:kAdaptedFontSize(13)
                                                              lineSpace:5
                                                    firstLineHeadIndent:0
                                                              alignment:NSTextAlignmentLeft];
    }
    
    [self rd_userInteractionEnabled];
}
#pragma mark -- 文字设置
// 首行缩进及换行文字左对齐
+ (NSMutableAttributedString *)stringWithText:(NSString *)text
                                    textColor:(UIColor *)textColor
                                         font:(UIFont *)font
                                    lineSpace:(CGFloat)lineSpace
                          firstLineHeadIndent:(CGFloat)firstLineHeadIndent
                                    alignment:(NSTextAlignment)alignment{
    
    NSMutableAttributedString *attrStr0 = [[NSMutableAttributedString alloc] initWithString:text];
    [attrStr0 addAttribute:NSForegroundColorAttributeName
                     value:textColor
                     range:NSMakeRange(0, text.length)];
    [attrStr0 addAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, text.length)];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineSpacing = lineSpace;// 字体的行间距
    paragraph.firstLineHeadIndent = firstLineHeadIndent;//首行缩进
    paragraph.alignment = alignment;
    
    [attrStr0 addAttribute:NSParagraphStyleAttributeName
                     value:paragraph
                     range:NSMakeRange(0, [text length])];
   
    return attrStr0;
}

你可能感兴趣的:(iOS _UITextView限制输入字符数量与行间距设置)