Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set height of UIWebView based on html content?

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.

like image 897
KPIteng Avatar asked Oct 16 '12 12:10

KPIteng


2 Answers

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.

like image 168
Guntis Treulands Avatar answered Sep 18 '22 22:09

Guntis Treulands


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;
}
like image 24
Mick MacCallum Avatar answered Sep 18 '22 22:09

Mick MacCallum