17

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.

Venk
  • 5,949
  • 9
  • 41
  • 52
KPIteng
  • 175
  • 1
  • 1
  • 6

5 Answers5

13

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.

Sujay
  • 2,510
  • 2
  • 27
  • 47
Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
  • 3
    Why are you iterating over the subviews to find the scroll view? just use webView.scrollView. I also think that you should only set bounces in viewDidLoad since it only needs to happen once. (It's still a fine answer.) – griotspeak Aug 27 '14 at 12:35
  • Thanks for the comment - good idea :) . The code I posted is really old, from time when scrollview was not accessible directly. – Guntis Treulands Aug 27 '14 at 14:35
8

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;
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
6

Try this code

- (void)webViewDidFinishLoad:(UIWebView *)webView
{     
    height= [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
}
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
1
- (void)viewDidLoad
{
    [super viewDidLoad];
    webview.delegate = self;
    [webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
    NSLog(@"height: %@", output);
}

see also How to determine UIWebView height based on content, within a variable height UITableView?

Calculate UIWebView height depending on its content

Community
  • 1
  • 1
Venk
  • 5,949
  • 9
  • 41
  • 52
  • 2
    But i have UIWebview in UITableView & each row have UIWebView so how i calculate because table height set before cellForRowAtIndex called. – KPIteng Oct 29 '12 at 10:46
0

If you have webview in tableviewcell, in heightForRowAtIndexPath set the height of cell to height of NSAttributedString size as following.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    NSAttributedString *attributedText = [NSString  getHTMLAttributedString:@"HTML Content"];
    CGSize size = CGSizeMake(300, CGFLOAT_MAX);
    CGRect paragraphRect =     [attributedText boundingRectWithSize:size
                                 options:(NSStringDrawingUsesLineFragmentOrigin)
                                 context:nil];
    return paragraphRect.size.height;
}

+(NSAttributedString *) getHTMLAttributedString:(NSString *) string{
    NSError *errorFees=nil;
    NSString *sourceFees = [NSString stringWithFormat:
                            @"<span style=\"font-family: 'Roboto-Light';font-size: 14px\">%@</span>",string];
    NSMutableAttributedString* strFees = [[NSMutableAttributedString alloc] initWithData:[sourceFees dataUsingEncoding:NSUTF8StringEncoding]
                                                                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                           NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                                      documentAttributes:nil error:&errorFees];
    return strFees;

}
Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55