1

I have a UIWebView where I load HTML content (stylised using CSS). I wish to set the correct frame (specifically height) for the webView in the webViewDidFinishLoad method. I'm going nuts. Is there no standard/uniform way to figure out the HTML content's height?

Specifically the height required resides within a div named "ABC". The closest to the correct height is:

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"ABC\").offsetHeight;"].intValue)]

I've tried clientHeight and scrollHeight for the above. I've also tried document.body.client/scroll/offsetHeight. None of the above give me a perfect height value. Everything results in some part of the webview getting cut off as the resultant height is lesser than the required value. How do you solve this thing?

Bourne
  • 10,094
  • 5
  • 24
  • 51

2 Answers2

0

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

OR

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

int height = [result integerValue];

you can call this after html completely loaded..i.e in webViewDidFinishLoading delegate

faiziii
  • 371
  • 4
  • 11
0

Rather late ;) but there weren't many answers, so I'll add my few cents.

Is there no standard/uniform way to figure out the HTML content's height?

AFAIK - No. :)

I fought with this one for a loooong time about a year ago. Key thing is - webViewDidFinishLoading is called when the webview is done loading, not rendering the content. It's especially visible when you have some images in the content loaded by the webview. I remember I got somewhat-acceptable results after taking the following, very hacky approach:

The page loaded by the webview was loaded from the local storage and used jQuery. In the WebView's javascript environment, I was listening for $(window).load() event http://api.jquery.com/load-event/, after receiving which I was calling the native side of the app (you can use a library for that, e.g. iOS JavaScript bridge), passing current content height as a call parameter.

Back then I got somewhat-acceptable results (some glitches still occurred from time to time). In general - one shouldn't even begin to deal with that problem. It's very hacky and won't be reliable. Many people try to use WebView as a "super-label". If it's viable at all - it's better to check out TextKit. One can do pretty awesome stuff with it.

Community
  • 1
  • 1
kajot
  • 303
  • 1
  • 9