美文网首页
UIWebView加载HTML标签

UIWebView加载HTML标签

作者: 倪大头 | 来源:发表于2019-06-05 17:06 被阅读0次

遵循UIWebViewDelegate协议,在代理方法里获取内容整体高度,调整 webview的frame

NSString *htmlStr = "<p><img src=\"https://img20.360buyimg.com/vc/jfs/t1/37127/23/887/799049/5cadd35dE9ec3cc24/766b05c2232b3b23.jpg\" style=\"max-width:100%;\"><br></p><p><img src=\"https://img20.360buyimg.com/vc/jfs/t1/36403/9/5981/4667392/5cc17b58E75c1b2d8/19f52d37e5813053.jpg\" style=\"max-width:100%;\"><br></p>";
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(kScaleX * 15, 0, kScaleX * 345, 10)];
webView.delegate = self;
webView.userInteractionEnabled = NO;
[webView loadHTMLString:htmlStr baseURL:nil];
[_myScrollView addSubview:webView];

代理:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *contentH = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
    webView.frame = CGRectMake(kScaleX * 15, 0, kScaleX * 345, contentH.floatValue);
    
    _myScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(webView.frame) + 20);
}

相关文章

网友评论

      本文标题:UIWebView加载HTML标签

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