UITextView加载html字符串,图片不能自适应的解决方法

UITextView中,加载服务器返回的html字符串,用到的方法为

+ (NSMutableAttributedString *)attributedStringWithHTMLString:(NSString *)htmlStr withFont:(UIFont *)font {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[[NSString stringWithFormat:@" %@",htmlStr] dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedString.length)];
    return attributedString;
}

但是图片没有展示完全


image.png

添加图片自适应的html语句

    NSString *adaptString = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@", self.contentTextView.width, self.currentVideo.content];

这样图片显示就正常了


image.png

参考文档:
https://www.jianshu.com/p/7d604f360d9a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容