美文网首页
iOS 获取 webView 加载完成后的高度2

iOS 获取 webView 加载完成后的高度2

作者: 三岁就很乖 | 来源:发表于2020-06-09 14:34 被阅读0次
效果图.png

iOS webView 加载HTML5获取加载后的高度
奇葩的事儿很多,比如这样 下边的图文详情 数据来源有些复杂是一些HTML字符串 ,我们需要用webview加载这些字符串 获取最终webView的高度。因为此视图需要加载到tableView的cell上 ,所以需要在获取到webView的高度后 重新刷新tableView 。

(void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
//HTML5的高度 
NSString *htmlHeight = [webView stringByEvaluatingJavaScriptFromString:@”document.body.scrollHeight”]; 
//HTML5的宽度 
NSString *htmlWidth = [webView stringByEvaluatingJavaScriptFromString:@”document.body.scrollWidth”]; 
//宽高比 
float i = [htmlWidth floatValue]/[htmlHeight floatValue];

//webview控件的最终高度 
float height = screenWidth/I;

//后面的代码 
//重新设置webView的高度,刷新tableView 设置cell的高度 
_tuwenWeb.frame = CGRectMake(0, 0, self.view.frame.size.width, height); 
[self.tableView reloadData]; 
。。。。。。

} 
//设置cell的高度 
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath 
{ 
return _tuwenWeb.scrollView.contentSize.height; 
} 

废话说完了 以下是找到对于我这个项目最有效的方法:

我是一个搬运工~~
来源:感谢🙏
https://blog.csdn.net/foxi_777/article/details/78831402?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-18.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-18.nonecase

第一种

//html5 图片设置为屏幕宽
NSString *myStr = [NSString stringWithFormat:@"<head><style>img{max-width:%f !important;}</style></head>",screenWidth];
NSString *str = [NSString stringWithFormat:@"%@%@",myStr,html5文本];

//////////////////////////////////////////////
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   //HTML5的高度
   NSString *htmlHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
   //HTML5的宽度
   NSString *htmlWidth = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"];
  //宽高比
   float i = [htmlWidth floatValue]/[htmlHeight floatValue];
  
   //webview控件的最终高度
   float height = screenWidth/i;

   //后面的代码
   。。。。。。
   
}

第二种

        NSString *htmls = [NSString stringWithFormat:@"<html> \n"
                           "<head> \n"
                           "<style type=\"text/css\"> \n"
                           "body {margin:18;font-size:45;color:0x666666}\n"
                           "</style> \n"
                           "</head> \n"
                           "<body>"
                           "<script type='text/javascript'>"
                           "window.onload = function(){\n"
                           "var $img = document.getElementsByTagName('img');\n"
                           "for(var p in  $img){\n"
                           " $img[p].style.width = '100%%';\n"
                           "$img[p].style.height ='auto'\n"
                           "}\n"
                           "}"
                           "</script>%@"
                           "</body>"
                           "</html>",_model.content];
        [_webView loadHTMLString:htmls baseURL:nil];
 
 
 
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    NSString *htmlHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
    NSString *htmlWidth = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"];
    
    float i = [htmlWidth floatValue]/[htmlHeight floatValue];
  
  float height = screenWidth/i;
    //
    //后面的代码
    。。。。。。
 
}
 

相关文章

网友评论

      本文标题:iOS 获取 webView 加载完成后的高度2

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