获取UIWebView内容的高度

24

我有一个 UIWebView,使用 loadHtmlString 函数加载数据。问题是,我从 SQLite 数据库加载数据,每次加载不同长度的不同字符串,因此 WebView 自然会获得不同高度。
现在我需要每次准确获取 UIWebView 的高度,以便在 WebView 正下方加载一个 ImageView。

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;

    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"Size: %f, %f", fittingSize.width, fittingSize.height);
}

但我总是得到相同的结果。我需要知道如何在每次加载网页视图时动态获取其高度。谢谢。

3个回答

47

试试这个

NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

int height = [result integerValue];

在你的webViewDidFinishLoad方法中。


我该如何将结果存储到变量中?我没有看到您的建议的结果,我该如何处理它?我是新手,谢谢您的理解。 - Elias Rahme
2
@EliasRahme 你可以将结果存储在返回的字符串中:NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]; 要将 result 字符串转换为整数,可以使用:int height = [result integerValue]; - Alexandre OS
13
[webView stringByEvaluatingJavaScriptFromString:@"Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );"]这段代码是用于获取当前网页的最大高度,包括body和html标签的高度。 - iMx
@iMx,你的评论起作用了,你能稍微解释一下为什么我们需要获取所有值的最大值吗? - Thomas Besnehard
@Tommecpe 我现在不确定为什么要在iOS中使用这个解决方案,我想我不得不扩展代码,因为它在旧的iOS版本中不起作用(我的应用程序的部署目标是4.3)。HTML/JS中没有标准的文档/页面高度,目前受到浏览器的影响。这是一个跨浏览器的解决方案。 - iMx
显示剩余2条评论

29

您可以通过访问 Web 视图的 scrollView 属性来获取内容的高度:

NSLog(@"%f",myWebView.scrollView.contentSize.height);

1
比 JS better,+1 給你。 - user529758
8
我尝试了,但是仍然得到相同的数字……即使我只有一个两个单词的段落,我也得到了737,所以它没有起作用:S - Elias Rahme
@EliasRahme,您能否更好地解释一下您在这里试图实现什么。我上面列出的代码就是您跟踪Web视图中内容大小的方式。 - Mick MacCallum
当网页视图加载一段文字时...由于我使用了[self.WebV setScalesPageToFit:YES];,它会自动增加高度。因此,每次它加载一段文字时,我需要获取其精确的新高度,以便在其正下方放置一个图像视图。 - Elias Rahme
2
这对我也不起作用,但 JS 注入起作用了。我还加载了长文本和多个段落。 - Matthew Quiros
此高度不可超过UIWebView的框架高度。 - liruqi

-5
这不是处理这种情况的正确方法;与其在图像视图中加载图像并始终记住高度、宽度和所有相关内容...最简单、最可靠的解决方案就是在html字符串中加载图像,就这样!只需确保baseURL如此,无论您采取什么方向,一切都会没问题:
 NSString* extension = @"gif";
    NSString* strRR = [NSString stringWithFormat:@"%@.%@", authorNAme2,extension];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    //NSLog(@"This is the path %@", path);
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    //  NSLog(@"this is the base URL %@",baseURL);

    NSString *cachePath = [NSString stringWithFormat:@"%@/%@", path,strRR];

            NSString * htmlString = [NSString stringWithFormat:@"\
                                     <html>\
                                     <table width='100%%'>\<tr>\<td>\
                                     <body>\
                                     <p style = 'font-size:30px;'> %@ <\p>\
                                     </td></tr><tr><td><br></td></tr><tr><td align ='center'><br><img src='%@' width='60%%' \></td></tr></body>\
                                     </table>\
                                     </html>",authorNAme , cachePath];



            //          NSString *selection = [WebV2 stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
            //          NSLog(@"*/*/*/*/**/*/*/*/*/*/*//*/ This is your selection mate %@" , selection);




            WebV.opaque = NO;
            WebV.backgroundColor = [UIColor clearColor];  
            [self.WebV setScalesPageToFit:YES];
            [self.WebV loadHTMLString:htmlString baseURL:baseURL];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接