24

I have a uiwebview that loads data using the loadHtmlString function.The thing is i am loading the data from an sqlite database, each time i load a different string with different length and naturally the web view obtains different height.
Now i need to know each time the exact height of the uiwebview in order to load an imageView right under the webView. I have tried

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;

    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"Size: %f, %f", fittingSize.width, fittingSize.height);
}

but i always get the same result. I need to know how can i get dynamically the height of web view each time it is loaded. Thx

Max P
  • 25
  • 6
Elias Rahme
  • 2,226
  • 3
  • 28
  • 53

3 Answers3

47

Try this

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

int height = [result integerValue];

in your webViewDidFinishLoad method.

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
Sumit Mundra
  • 3,891
  • 16
  • 29
  • Was going to suggest this. +1. –  Oct 29 '12 at 14:01
  • And how am i going to get the result in a variable? I don't see the result of your suggestion how can i handle it ? i am new thanks for understanding – Elias Rahme Oct 29 '12 at 14:11
  • 2
    @EliasRahme You get the result into the returned string: `NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];` To convert the `result` string into an int you can use: `int height = [result integerValue];` – Alexandre OS Jan 21 '13 at 18:22
  • 13
    [webView stringByEvaluatingJavaScriptFromString:@"Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );"] – iMx Jul 11 '13 at 14:22
  • @iMx you comment work perfectly, can you explain a bit why do we need to get the max of all those value ? – Thomas Besnehard Jan 21 '14 at 17:37
  • @Tommecpe I'm not sure now why exactly I used this solution in iOS, i guess I had to extend the code because it didn't work in older iOS versions (my application's deployment target was 4.3). The document/page height has no standard in HTML/JS and is currently subject to browser. This is a cross-browser solution. – iMx Jan 22 '14 at 09:55
  • In my case JS injection did the best. Thanks @iMx – csonuryilmaz Jan 10 '17 at 13:38
29

You can get the height of the content in a web view by accessing the web view's scrollView property:

NSLog(@"%f",myWebView.scrollView.contentSize.height);
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • 1
    Better than JS insection. +1 for you too. –  Oct 29 '12 at 14:01
  • 8
    I tried it but the same number keeps on going on...even if i have a 2 words paragraph i get 737 so it didn't work :S – Elias Rahme Oct 29 '12 at 14:10
  • @EliasRahme Can you please better explain what you are trying to achieve here. The code I've listed above *is* how you track the size of the content in a webview. – Mick MacCallum Oct 29 '12 at 14:12
  • When the web view loads a paragraph of words...it will automatically grow in height since i am using [self.WebV setScalesPageToFit:YES]; So i need at each time it loads a paragraph of words..i get the exact new height of it in order to place an image view right underneath her – Elias Rahme Oct 29 '12 at 14:15
  • 2
    This didn't work for me too and the JS injection did. I'm also loading long texts and multiple paragraphs. – Matthew Quiros Apr 03 '14 at 07:53
  • This height cannot be higher than UIWebView's frame height. – liruqi Aug 17 '17 at 10:10
-5

This wasn't the right approach to handle this situation; instead of loading the image in an image view and having to keep always in mind the height and width and all that stuff...The easiest and most reliable solution was just to load the image inside the html string and that's it ! Just be sure of the baseURL as so and everything will be ok no matter what orientation you take :

 NSString* extension = @"gif";
    NSString* strRR = [NSString stringWithFormat:@"%@.%@", authorNAme2,extension];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    //NSLog(@"This is the path %@", path);
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    //  NSLog(@"this is the base URL %@",baseURL);

    NSString *cachePath = [NSString stringWithFormat:@"%@/%@", path,strRR];

            NSString * htmlString = [NSString stringWithFormat:@"\
                                     <html>\
                                     <table width='100%%'>\<tr>\<td>\
                                     <body>\
                                     <p style = 'font-size:30px;'> %@ <\p>\
                                     </td></tr><tr><td><br></td></tr><tr><td align ='center'><br><img src='%@' width='60%%' \></td></tr></body>\
                                     </table>\
                                     </html>",authorNAme , cachePath];



            //          NSString *selection = [WebV2 stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
            //          NSLog(@"*/*/*/*/**/*/*/*/*/*/*//*/ This is your selection mate %@" , selection);




            WebV.opaque = NO;
            WebV.backgroundColor = [UIColor clearColor];  
            [self.WebV setScalesPageToFit:YES];
            [self.WebV loadHTMLString:htmlString baseURL:baseURL];
Elias Rahme
  • 2,226
  • 3
  • 28
  • 53