第一种方法:
-
-
(void)webViewDidFinishLoad:(UIWebView *)webView -
{
-
CGFloat height = [[webView stringByEvaluatingJavaSc riptFromString:@"document.body.offsetHeight"] floatValue]; -
CGRect frame = webView.frame; -
webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height); - }
第二种方法:
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight=[webView.scrollView contentSize].height;
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
}
CGFloat webViewHeight=[webView.scrollView contentSize].height;
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
}
【注】:第一种方法不知道为什么计算出来的高度总是少大约10-20的距离;
在iOS开发中,使用UIWebView加载网页内容时,通过 webViewDidFinishLoad 方法,结合 JavaScript 计算 document.body.offsetHeight 来获取网页内容的实际高度,并设置为 webview 的高度,实现文本内容的自适应布局。
1272

被折叠的 条评论
为什么被折叠?



