美文网首页iOS
iOS textView 渲染html富文本&计算高度&图片适配

iOS textView 渲染html富文本&计算高度&图片适配

作者: iOS_July | 来源:发表于2019-08-08 10:55 被阅读0次
    • 先看效果:
      text.gif
    • 两个关键方法,传参时记得统一,也可以自己略作调整
    /**
     html 富文本设置
    
     @param str html 未处理的字符串
     @param font 设置字体
     @param lineSpacing 设置行高
     @return 默认不将 \n替换<br/> 返回处理好的富文本
     */
    -(NSMutableAttributedString *)setAttributedString:(NSString *)str font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing
    {
        //如果有换行,把\n替换成<br/>
        //如果有需要把换行加上
    //    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
        //设置HTML图片的宽度
        str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",[UIScreen mainScreen].bounds.size.width,str];
        NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
        //设置富文本字的大小
        [htmlString addAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, htmlString.length)];
        //设置行间距
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:lineSpacing];
        [htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [htmlString length])];
        
        return htmlString;
    
    }
    
    
    
    /**
     计算html字符串高度
    
     @param str html 未处理的字符串
     @param font 字体设置
     @param lineSpacing 行高设置
     @param width 容器宽度设置
     @return 富文本高度
     */
    -(CGFloat )getHTMLHeightByStr:(NSString *)str font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing width:(CGFloat)width
    {
    //    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
        str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",[UIScreen mainScreen].bounds.size.width,str];
        
        NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
        [htmlString addAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, htmlString.length)];
        //设置行间距
        NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:lineSpacing];
        [htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [htmlString length])];
        
        CGSize contextSize = [htmlString boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
        return contextSize.height ;
        
    
    }
    
    • 使用:
    CGFloat detailY = CGRectGetMaxY(self.padView.frame);
        
        NSAttributedString *attributedString = [self setAttributedString:self.model.Details font:kHMFont(16) lineSpacing:Height_Real(10)];
        CGFloat textHeight = [self getHTMLHeightByStr:self.model.Details font:kHMFont(16) lineSpacing:Height_Real(10) width:SCREEN_WIDTH];
        
        UITextView *detailView = [[UITextView alloc]initWithFrame:CGRectMake(0, detailY, SCREEN_WIDTH, textHeight)];
        detailView.attributedText = attributedString;
        detailView.scrollEnabled = NO;
        detailView.editable = NO;
        [self.scrollView addSubview:detailView];
    

    相关文章

      网友评论

        本文标题:iOS textView 渲染html富文本&计算高度&图片适配

        本文链接:https://www.haomeiwen.com/subject/xybtjctx.html