ios 获取 UIWebView 内容的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13123009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:59:13  来源:igfitidea点击:

Get UIWebView content's Height

iosuiwebviewheightxcode4.3

提问by Elias Rahme

I have a uiwebview that loads data using the loadHtmlString function.The thing is i am loading the data from an sqlite database, each time i load a different string with different length and naturally the web view obtains different height.
Now i need to know each time the exact height of the uiwebview in order to load an imageView right under the webView. I have tried

我有一个使用 loadHtmlString 函数加载数据的 uiwebview。问题是我从 sqlite 数据库加载数据,每次加载不同长度的不同字符串时,Web 视图自然会获得不同的高度。
现在我每次都需要知道 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);
}

but i always get the same result. I need to know how can i get dynamically the height of web view each time it is loaded. Thx

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

采纳答案by Elias Rahme

This wasn't the right approach to handle this situation; instead of loading the image in an image view and having to keep always in mind the height and width and all that stuff...The easiest and most reliable solution was just to load the image inside the html string and that's it ! Just be sure of the baseURL as so and everything will be ok no matter what orientation you take :

这不是处理这种情况的正确方法。而不是在图像视图中加载图像并且必须始终牢记高度和宽度以及所有这些东西......最简单和最可靠的解决方案就是将图像加载到 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];

回答by Sumit Mundra

Try this

尝试这个

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

int height = [result integerValue];

in your webViewDidFinishLoad method.

在您的 webViewDidFinishLoad 方法中。

回答by Mick MacCallum

You can get the height of the content in a web view by accessing the web view's scrollViewproperty:

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

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