1

I work for a media company, and in our iPad application we're upgrading our content so it looks better. Previously, we used a UILabel to display the contents of our posts. I used this to get the actual height of my UILabel:

CGSize expectedSize = [label.text sizeWithFont:label.font
    constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];

However, we changed our posts' content to HTML, so I'm using a UIWebView. Is there a way to find the true height of the UIWebView, like I do with the UILabel?

The WebMacheter
  • 1,776
  • 1
  • 20
  • 31
  • Do you mean the height of the document that has been rendered by the UIWebView or the height of the UIWebView control itself? – Evan Mulawski Nov 09 '10 at 14:43

1 Answers1

4

Unlike with a plain string, you'll probably only be able to get the height once the web view finishes rendering. Here's how you can do it in a web view delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webview
{
    NSString *heightString = [webview stringByEvaluatingJavaScriptFromString:
                              @"document.body.clientHeight"];
    int height = [heightString intValue];
    ...
}
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • 1
    Awesome, thanks! however, I have a little correction for your suggestion. Following advice from http://stackoverflow.com/questions/294250/how-do-i-retreive-an-html-elements-actual-width-and-height I used offsetHeight instead of clientHeight, since clientHeight didn't work for me. – The WebMacheter Nov 09 '10 at 15:58
  • 2
    Great. How about using scrollHeight? – Seunghoon Mar 16 '11 at 03:02