Calculate UIWebview height in UITableViewCell with

2019-05-21 12:26发布

I'm loading an html to a webview, that can contain embed links to twitter, vimeo, youtube, or just plain simple html.

Just for test purpose, i've created a simple view using the same way i'm calculating the height, and i have a table view the is dependent of the height calculated to show the webview correctly.

The problem is that calculated heights are wrong, so embed html is not displayed in correctly.

This are the relevant prats of my code:

-> This loads html string in dummy webview, the only purpose it to calculate correct height, the dummy view is used to trigger load of html:

    - (void) loadHtml:(NSIndexPath *) indexPath andString:(NSString *) htmlString {

        CGFloat webViewFrame = 216 * [[UIScreen mainScreen] bounds].size.width / 320;

        wvVtemp = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, webViewFrame, 5)];
        wvVtemp.delegate = self;
        [wvVtemp loadHTMLString:htmlString baseURL:nil];

        [dummyView addSubview:wvVtemp];
        [dummyView bringSubviewToFront:wvVtemp];

    } 

-> WebView delegate, is the webview is not loading it will fire the method to calculate height:

    #pragma mark - uiwebview delegate
    - (void)webViewDidStartLoad:(UIWebView *)webView {
    }


    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

        if (!webView.isLoading) {
            [self getHeightForWebViewT:webView];
        }  
    }


    - (void)webViewDidFinishLoad:(UIWebView *)webView;{

        if (!webView.isLoading) {
            [self getHeightForWebViewT:webView];
        }
    }

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        return true;
    }

-> Calculate the height of webview:

    - (void) getHeightForWebViewT:(UIWebView *) webView {

        [webView sizeToFit];

        NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
        calculatedHeight = [output floatValue];

        NSLog(@"Calculated indexPath wvTemp:  -> height: %@",  output);
        //[self.tView reloadRowsAtIndexPaths:@[webView.indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tView reloadData];
    } 

-> The HTML i'm rendering:

    <html><body>Begin
    <blockquote class="twitter-tweet" lang="en">
    <p dir="ltr" lang="en">Apple seeds OS X El Capitan beta 7 to developers <a href="http://t.co/DBhpDdyXKU">http://t.co/DBhpDdyXKU</a> <a href="http://t.co/sWkTcOVMvt">pic.        twitter.com/sWkTcOVMvt</a></p>
    &mdash; iDownloadBlog (@iDownloadBlog) <a href="https://twitter.com/iDownloadBlog/status/634096776107048961">August 19, 2015</a></blockquote>
    <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><br />
    End</body></html>

Currently this solution is not working only with twitter embed code, and the final layout is this (it's missing an "End" from html)

screen shoot

Another issue with this is that not long ago this worked correctly, in the last weeks it start to work only from time to time, and now the output is always the same.

I've tried all solutions i've found in google and the i could remember of, but currently i'm stuck in this... any help/tip would be very appreciated.

1条回答
闹够了就滚
2楼-- · 2019-05-21 12:57

You can get it directly, without using Javascript

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   // webViewHeight = webView.scrollView.contentSize.height;
}

If you want to use Javascript, try replacing document.body.scrollHeight with document.body.offsetHeight

查看更多
登录 后发表回答