美文网首页
UILabel attributedText html 图片自适

UILabel attributedText html 图片自适

作者: CoderBigBear | 来源:发表于2017-09-21 17:04 被阅读101次

    -(void)resetDetailHtml{
    //图片自适应宽高,只限制图片的最大显示宽度,这样就能做到自适应
    NSString newString =[NSString stringWithFormat:@"<html>"
    "<head>"
    "</style>"
    "<style>
    {margin:3px 0px 3px 0px;padding:0 ;max-width:%f;}</style>"
    "</head>"
    "<body>%@</body>"
    "</html>",self.view.frame.size.width-30,_detailHtmlData];
    NSData *data = [newString dataUsingEncoding:NSUnicodeStringEncoding];
    NSDictionary *options = @{
    NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
    };
    NSMutableAttributedString *htmlAttribute = [[NSMutableAttributedString alloc] initWithData:data
    options:options
    documentAttributes:nil
    error:nil];
    //设置字体
    autoSize6
    UIFont baseFont = [UIFont systemFontOfSize:15aSX];
    [htmlAttribute addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, htmlAttribute.length)];//设置所有的字体
    _detailView.attributedText = htmlAttribute;
    CGRect frame = _detailView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [_detailView sizeThatFits:constraintSize];
    if (size.height<=frame.size.height) {
    size.height=frame.size.height;
    }
    _detailView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
    }

    相关文章

      网友评论

          本文标题:UILabel attributedText html 图片自适

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