Determining content size

suggest change

In many cases, for instance when using web views in table view cells, it’s important to determine the content size of the rendered HTML page. UIWebView is deprecated. This measurement is the UIWebViewDelegate method, called after the page loads:

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

The code briefly sets the web view’s height to 1 before measuring. Otherwise sizeThatFits: reports the current frame size. frame.size = fittingSize then replaces both the width and the height with that measured size.

Source

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents