计算 UIWebView 里html内容高度

//创建webView,显示html内容

UIWebView*webView = [[UIWebViewalloc]init];

[webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.cpDescUrl]]];

webView.scalesPageToFit=YES;

webView= webView;

webView.delegate=self;

//设置UIWebView高度等于html内容高度

//通过代理获取页面高度

- (void)webViewDidFinishLoad:(UIWebView*)webView {

//方法一:

CGFloat documentHeight= [[wb stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];

//方法二:

CGRect frame = webView.frame;

frame.size.width=screenWith;

frame.size.height=1;//这步不能少,不然webView.scrollView.contentSize.height为零

webView.frame= frame;

frame.size.height= webView.scrollView.contentSize.height;

webView.frame= frame;

self.scrollView.contentSize=CGSizeMake(screenWith,self.contentHeight+frame.size.height+kMargin);

webView.scrollView.scrollEnabled=NO;

}

//原理:

方法一通过调用js来获取html的body内容高度。

方法二通过获取webview自适应设置的contentsize.height来获得内容高度,但要注意在获取正确contentsize.height时,webview的frame不能为CGRectZero。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容