获取UIWebView内容的高度

Eli*_*hme 24 height uiwebview ios xcode4.3

我有一个使用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);
}
Run Code Online (Sandbox Code Playgroud)

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

Sum*_*dra 47

试试这个

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

int height = [result integerValue];
Run Code Online (Sandbox Code Playgroud)

在您的webViewDidFinishLoad方法中.

  • [webView stringByEvaluatingJavaScriptFromString:@"Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );"] (13认同)
  • @EliasRahme您将结果输入到返回的字符串中:`NSString*result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];`要将`result`字符串转换为int,您可以使用:`int height = [result integerValue];` (2认同)

Mic*_*lum 29

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

NSLog(@"%f",myWebView.scrollView.contentSize.height);
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了但是同样的数字一直在继续...即使我有一个2字段我得到737所以它不起作用:S (7认同)
  • 这对我来说也不起作用,并且JS注入了.我也在加载长文本和多段. (2认同)

Eli*_*hme -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];
Run Code Online (Sandbox Code Playgroud)