UIWebView加载HTML标签

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

NSString *htmlStr = "



"; 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标签)