5

In a UITextView i can resize the height of the control to it's content like this:

CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;

But i want to use an UIWebView instead in order to show rich text. The UIWebVie's content is:

NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                  "<head> \n"
                                  "<style type=\"text/css\"> \n"
                                  "body {font-family: \"%@\"; font-size: %@; }\n"
                                  "</style> \n"
                                  "</head> \n"
                                  "<body>%@</body> \n"
                                  "</html>", @"helvetica",
                                  [NSNumber numberWithInt:12], @"yadayadayada..."];
[webView loadHTMLString:myDescriptionHTML baseURL:nil];

So the UIWebView contains nothing but plain text. How can i achieve result similar to the above UITextView code?

ferostar
  • 7,074
  • 7
  • 38
  • 61
  • possible duplicate of [UIWebView resize to fit Content](http://stackoverflow.com/questions/5914472/uiwebview-resize-to-fit-content) – jscs May 24 '11 at 04:10

6 Answers6

14

Although in theory the JavaScript method provided

- (void)webViewDidFinishLoad:(UIWebView *)webview
    CGRect oldBounds = [[self webview] bounds];
    CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
    [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)];
}

should work (and seems to work for most people), it just didn't for me. I tried with a number of variation and it always returned a size that has nothing to do with the true content's height. A one liner would come up with 260 or 244 height, for instance. Don't know why (would love to know).

SO i ended up doing as hinted in this answer:

- (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);
}

I think the JS way more elegant but hell, it doesn't works for me and can't figure out why, while this method worked out of the box.

Community
  • 1
  • 1
ferostar
  • 7,074
  • 7
  • 38
  • 61
2

I know this is old but I could not find any more recent answers to my problem. I ended up with this sollution. This is valid when using constraints.

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    // get content size
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    // ajust the height constraint of the webview to the content size
    [self.webviewHeightConstraint setConstant:fittingSize.height];
}
speedrock
  • 115
  • 1
  • 7
2

Similar to what Micheal suggested, you should be able to get the height of the content by making a call to javascript in the webview's webViewDidFinishLoad callback

ex (untested):

- (void)webViewDidFinishLoad:(UIWebView *)webview
    CGRect oldBounds = [[self webview] bounds];
    CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
    [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)];
}

This is based on a method used in Appcelerator Titanium to resize its webviews based on height if the user sets height to "auto" https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TiUIWebView.m#L616

netpro2k
  • 141
  • 4
  • 1
    This will make your UIWebView larger if you call loadHTMLString:baseURL: a second time with a longer HTML page, but not smaller with a shorter HTML page. Use "document.body.clientHeight" for the JavaScript to shrink and grow the UIWebView height upon loading a new HTML string. – Mr. Berna May 24 '11 at 17:10
1

There is no simple answer to this, as a UIWebView is a complex scroll view. A few things you could try, all must be done after the UIWebView delegate gets notified that the webpage finished loading (using webViewDidFinishLoad:). Once the delegate knows that UIWebView is loaded, I would try this:

  • Try the same contentSize trick, unlikely that it will work, but it might if the page is loaded
  • Get the height using javascript, like this:

    CGFloat windowHeight = [[webView stringByEvaluatingJavaScriptFromString:@"return document.body.scrollHeight;"] floatValue];

Then do your magic with the height as you would with any other view.

Michael Petrov
  • 2,247
  • 15
  • 15
1

Is this what you are looking for ?? UIScrollView *scroll= [[webView subviews]objectAtIndex:0]; CGFloat flo= scroll.contentSize.height;

Priyanka
  • 152
  • 6
0

Maybe shorten the code?

_webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, 1);
CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
_webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, fittingSize.height);
chikuba
  • 4,229
  • 6
  • 43
  • 75