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

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

作者: 里克尔梅西 | 来源:发表于2021-04-21 13:38 被阅读0次

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

相关文章

网友评论

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

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