How to set height of UIWebView based on html content?

Spread the love

Question Description

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.

Practice As Follows

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.