stringByEvaluatingJavaScriptFromString识别webview内容高度

时间:2012-05-11 14:38:32

标签: objective-c xcode cocoa ios5

我在UIscrollview中放置了一个Uiwebview,因为在uiwebview之后我想用PageController显示一些图像。

我正在使用这行代码。

   -(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (webView!=WebView)return;
    float h;

    NSLog(@"web view is %f high", WebView.frame.size.height);
    NSString *heightString = [WebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
    NSLog(@"web content is %@ high",heightString);

    h = [heightString floatValue] +12.0f;   
    WebView.frame = CGRectMake(WebView.frame.origin.x, WebView.frame.origin.y, WebView.frame.size.width, h);

    h = WebView.frame.origin.y + h + 20; 
    [ScrollView setContentSize:CGSizeMake(320, h)];


}

但是Web view is %f high不起作用,它没有重新确定WebView的高度吗?有谁知道我可以解决这个问题?这是因为我正在使用[WebView loadHTMLString:html baseURL:nil];

并且:NSString *heightString = [WebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];

 NSString *html = [NSString stringWithFormat:@"<html><head><!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><style>body {  font: normal 13px helvetica; color: #565656;   } </style></head><body><p>%@</p></body></html> ",text];  


[WebView loadHTMLString:html baseURL:nil];

2 个答案:

答案 0 :(得分:1)

尝试

NSString *heightString = [Bericht stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
祝你好运, 森

答案 1 :(得分:0)

UIWebView已包含scrollView,为什么要再次将其添加为子视图。查看Apple的文档here