超長文本處理UITextView

UITextView *textState = [[UITextView alloc] init];
    textState.scrollEnabled = YES;
    textState.userInteractionEnabled = YES;
    CGSize size = [self labelAutoCalculateRectWith:textState.text FontSize:18 MaxSize:CGSizeMake(300/320.0*SCREEN_WIDTH, 15000)];
    
    textState.frame = CGRectMake(0, STATUS_H, W, H - STATUS_H);
    textState.contentSize = size;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 6; //行距
    
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragraphStyle};
    
    textState.attributedText = [[NSAttributedString alloc]initWithString: STATEMENT_TXT attributes:attributes];
    [textState  scrollRectToVisible:CGRectMake(5, 100,textState.contentSize.width, 10) animated:YES];
    
    [self.view addSubview:textState];
//計算方法
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize

{
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc ]init];
    paragraphStyle.lineSpacing = 8;
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    CGSize labelSize = [text boundingRectWithSize:maxSize  options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    
    labelSize.height=ceil(labelSize.height);
    
    labelSize.width=ceil(labelSize.width);
    
    return labelSize;
    
}
相關文章
相關標籤/搜索