Determining content size
suggest changeIn 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents