Skip to content

jiutianhuanpei/ForthwithChangeTextViewHeight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

ForthwithChangeTextViewHeight

实时改变TextView的高度

- (CGFloat)reckonTextViewHeight:(UITextView *)textView {
    CGRect frame = textView.bounds;
    CGSize fudgeFactor = CGSizeMake(10.0, 16.0);
    
    frame.size.height -= fudgeFactor.height;
    frame.size.width -= fudgeFactor.width;
    
    NSMutableAttributedString* textToMeasure;
    if(textView.attributedText && textView.attributedText.length > 0){
        textToMeasure = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
    } else{
        textToMeasure = [[NSMutableAttributedString alloc] initWithString:textView.text];
        [textToMeasure addAttribute:NSFontAttributeName value:textView.font range:NSMakeRange(0, textToMeasure.length)];
    }
    
    if ([textToMeasure.string hasSuffix:@"\n"]) {
        [textToMeasure appendAttributedString:[[NSAttributedString alloc] initWithString:@"-" attributes:@{NSFontAttributeName: textView.font}]];
    }
    
    CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                              context:nil];
    
    CGFloat newHeight = CGRectGetHeight(size) + fudgeFactor.height;
    
    return newHeight;
}

About

实时改变TextView的高度

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published