I want to set height of UIWebview based on HTML content. I am getting the size of a string, but not getting the actual size due to paragraph, bold, different font size, etc.
I usually use these methods, to set UIWebview
frame as it's content size:
- (void)webViewDidStartLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 5.0f;
webView.frame = frame;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)]; // Pass about any size
CGRect mWebViewFrame = webView.frame;
mWebViewFrame.size.height = mWebViewTextSize.height;
webView.frame = mWebViewFrame;
//Disable bouncing in webview
for (id subview in webView.subviews) {
if ([[subview class] isSubclassOfClass: [UIScrollView class]]) {
[subview setBounces:NO];
}
}
}
They are automatically called (if you set webView
's delegate to this class), when WebView
has finished loading it's content.
Well, every web view has a UIScrollView built into it, so I would try waiting for the page to load and then tapping into the scroll view's contentSize property to get the height of the page.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = webView.scrollView.contentSize.height;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With