5

Possible Duplicate:
How to determine the content size of a UIWebView?

Hai Guys,

I got a problem while lodaing the htmlString into UIWebView, I have set the frame height of a webview in iphone is 265 always.

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 41, 320, 265)];

Is there any property to get content height of a UIWebView.

If I got the content only one or two lines then it is looking odd below the two lines. How can I make it dynamic. Please suggest any solution.

Thank you, Madan Mohan.

Community
  • 1
  • 1
Madan Mohan
  • 8,764
  • 17
  • 62
  • 96
  • You might wanna check [my answer](http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview/3937599#3937599) on a similar question that doesn't need any JavaScript. – Ortwin Gentz Dec 02 '10 at 20:29

1 Answers1

15

try this:

[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]

in your webViewDidFinishLoad method.

MaxFish
  • 449
  • 3
  • 7
  • 1
    I want to set the webView frame height if we know the height before loading. The string which is returning from given method is in piexls or what. – Madan Mohan Oct 01 '10 at 09:06
  • Sorry not to mention it. It's the height of the content in pixels. When you have that value you can set your webview frame height. I think there's no way to know the height before loading the page. – MaxFish Oct 01 '10 at 09:18
  • Sorry, Thanks for your response, But problem not solved. – Madan Mohan Oct 01 '10 at 09:26
  • The pixels I am getting is to large like 517, 233, if the content is only one line code – Madan Mohan Oct 01 '10 at 09:44
  • Does your problem is that you don't want to display the webview before knowing the exact size? – MaxFish Oct 01 '10 at 09:44
  • Even after loading i want to change the webview frame height, How can I – Madan Mohan Oct 01 '10 at 10:07
  • If you are loading page from a string you have to be sure it is a correct HTML document. This works: NSString * html = @" aaaaTest!"; – MaxFish Oct 01 '10 at 10:12
  • if the content height is less than webview frame height then this method always return the webview frame height :( so sad :( – Merkurial Jun 17 '16 at 10:35