UITextView 加载html文本(图片自适应)

WKWebView加载HTML文本

NSString * htmlsTr = @"哦吧你不会比谁都不休的白色修身<p>才能完呢冲破恩微博撇那狗骗你我饿够了呢比你耳边n伪军抹去你问邪恶</p><p><img src=\"http://yk3.gokuai.com/index/thumb?mount_id=1255485&amp;type=jpeg&amp;hash=c36c982f5d047666d7173c3625550152a2d403e6&amp;filehash=ca9ab472b4914bc0b3e7eefc9fa0fa66d75091bf&amp;big=1\" alt=\"Gravatar\" style=\"max-width:100%;display: block; position: relative; margin: auto;\"></p><p>热敷已成为8哦鳄鱼从背后有我饿</p><img src=\"http://yk3.gokuai.com/index/thumb?mount_id=1255485&amp;type=jpeg&amp;hash=565b67349cf2f08d4c373b350355adae46d7199c&amp;filehash=169585a54ecc8311159717a1f8334ba0045a7532&amp;big=1\" alt=\"Gravatar\" style=\"max-width:100%;display: block; position: relative; margin: auto;\"><p><a href=\"http://yk3.gokuai.com/file/hb14k046sx0l6gn2h471nkxm8i9zcau1#\" target=\"_blank\"><img src=\"http://dn-preview.gokuai.com/23/237b4494a9cc56b3c29dc1d34be8340774cf9ce6_1280.jpg\" alt=\"File\" style=\"max-width:100%;\"></a>去年细纹卡马乔全部下次去把握和刺猬比欧巴</p>";
NSString *str = [NSString stringWithFormat:@"<head><style>img{max-width:%f !important;height:auto}</style></head>%@",kScreenWidth-30,htmlsTr];
    UITextView * textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 100)];
    [self addSubview:textview];
    [textview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bottomView.mas_top).mas_offset(20);//10为按钮高度的一半
        make.left.mas_equalTo(self.bottomView.mas_left).mas_offset(10);
        make.right.mas_equalTo(self.bottomView.mas_right).mas_offset(-10);
        make.bottom.mas_equalTo(self.bottomView.mas_bottom).mas_offset(-10);
    }];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    textview.backgroundColor = [UIColor yellowColor];
    textview.attributedText = attributedString;

效果图

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