使用Javascript获取webView内容的总高度

时间:2010-06-05 09:47:50

标签: javascript iphone objective-c uiwebview uiscrollview

我正在使用UIWebView以HTML字符串的形式显示内容 - 而不是网站,高于iPhone的屏幕,而无需滚动webView本身,将其留在父scrollView。

要实现这一点,我需要一种方法来获取总文档大小,包括可滚动区域,以设置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 个答案:

答案 0 :(得分:18)

在iOS 5.0及更高版本中无需使用Javascript - 您可以直接访问其scrollView:

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

获取webView内容的总高度。

答案 1 :(得分:11)

当我尝试使用我的代码时,我找到了,

(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

答案 2 :(得分:6)

你在哪里打电话给你的代码? 对我来说,如果在loadHTMLString方法之后立即调用它,则返回0。 如果我在(void)webViewDidFinishLoad:(UIWebView *)theWebView委托中调用它,我会得到一个有效值。

- (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
}

答案 3 :(得分:6)

我采用了稍微不同的方法来创建一个< DIV>我可以通过关键来获得大小,因为我没有运气检查文件/正文。

这是MonoTouch 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);

并在LoadFinished中获得高度:

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

@Jesse,当使用HTML进行更新时,这会产生比以前显示的更小的尺寸。

相关问题