代码之家  ›  专栏  ›  技术社区  ›  Emil

使用javascript获取WebView内容的总高度

  •  9
  • Emil  · 技术社区  · 15 年前

    我用的是 UIWebView 对于显示内容,以HTML字符串“而不是网站”的形式,高于iPhone屏幕,无需在WebView中滚动,将其留给父滚动视图。

    要实现这一点,我需要一种方法来获取总文档大小,包括可滚动区域,以设置WebView的高度。我尝试过多种不同的javascript解决方案:

    (document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
    document.body.offsetHeight // Returns zero
    document.body.clientHeight // Returns zero
    document.documentElement.clientHeight // Returns height of UIWebView
    window.innerHeight // Returns height of UIWebView -2
    document.body.scrollHeight // Returns zero
    

    有没有真正有效的解决方案?

    电流(电流) 不工作的 代码:

    [[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
    int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue];
    NSLog(@"Content_height: %d", content_height);
    CGRect rect = self.singlePost.contentText.frame;
    rect.size.height = content_height;
    self.singlePost.contentText.frame = rect;
    
    4 回复  |  直到 12 年前
        1
  •  18
  •   Andres Kievsky    12 年前

    在iOS 5.0及更高版本中不需要使用javascript——您可以直接以文档形式访问其滚动视图:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        CGFloat contentHeight = webView.scrollView.contentSize.height;
        // ....
    }
    

    获取WebView内容的总高度。

        2
  •  11
  •   karim    13 年前

    当我尝试使用我的代码时,我发现,

    (1) NSLog(@"webView height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]);
    (2) NSLog(@"webView height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.height"]);
    

    (1) 返回低于原始高度的高度。但是 (2) 返回实际高度。我的建议是 Max 两者之中。

    - (void)webViewDidFinishLoad:(UIWebView *)theWebView {
        NSLog(@"Body height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]);
        NSLog(@"Doc height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.height"]);
        NSString * javaScript = [NSString stringWithFormat:@"document.getElementById('%@').clientHeight", kDivID];
        NSLog(@"Div height: %@",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
        [self reLayout];
    }
    
    - (CGFloat) getWebViewPageHeight {
        CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: @"document.height"] floatValue];
        CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] floatValue]; 
        return MAX(height1, height2);   
    }
    

    产量

    Body height: 485
    Doc height: 509
    Div height: 485
    
        3
  •  6
  •   robert    15 年前

    你在哪里打电话给你的密码? 对于我来说,如果在loadhtmlstring方法之后调用它,它将返回0。 如果我在(void)WebViewDidFinishLoad:(uiWebView*)WebView委托中调用它,我会得到一个有效值。

    - (void)loadHTML: (NSString *)html {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        webView.delegate = self;
    
        NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
        [webView loadHTMLString:html baseURL: resourceURL];
        NSLog(@"height: %d", [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue]); // returns 0
      }
    
    - (void)webViewDidFinishLoad:(UIWebView *)theWebView {
        NSLog(@"height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]); //return 2166
    }
    
        4
  •  6
  •   t9mike    13 年前

    我采用了一种稍有不同的方法,创建了一个<Div>,我可以通过键入来获取大小,因为我不太擅长检查文档/正文。

    这是单触式C,但在Objective-C中应该可以很好地工作:

    private const string DIV_ID = "_uiwebview_";
    
    LoadHtmlString("<body style='background-color:#yourcolor; padding:0px; margin:0px'>" +
      "<div style='width:" + Frame.Width + "px; padding:0px; margin:0px; font-family:" + 
      font.Name + "' id=" + DIV_ID + ">" + html + "</div></body>", base_url);
    

    完成装载高度:

    string sheight = EvaluateJavascript("document.getElementById('" + DIV_ID +
       "').clientHeight");
    

    @Jesse,这在使用HTML进行更新时有效,HTML的大小将比以前显示的要小。