【问题标题】:How Dynamically Chnage the height of webview According to HTML text如何根据 HTML 文本动态改变 webview 的高度
【发布时间】:2013-01-20 11:57:52
【问题描述】:

我想增加 webview 的大小,根据 Html 文本行,但我的 web view 大小没有正确,最初我在固定框架中显示内容,后来当用户选择行时我需要根据html文本,请帮我解决这个问题,我使用的代码是

enter code here  NSLog(@"web view and selected ==== %d ---%d",webView.tag,selectedRow);
//CGFloat webViewHeight = 0.0f;
if (webView.subviews.count > 0) {
    UIView *scrollerView = [webView.subviews objectAtIndex:0];
    if (scrollerView.subviews.count > 0) {
        UIView *webDocView = scrollerView.subviews.lastObject;
        if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])
            webViewHeight = webDocView.frame.size.height;

        NSLog(@"web view height is %d",webViewHeight);
    }

    }

【问题讨论】:

    标签: iphone uitableview uiwebview


    【解决方案1】:

    要获得实际的 HTML 文档高度,您应该这样做:

    NSUInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"getDocHeight()"] intValue];
    

    getDocHeight 在哪里:

    function getDocHeight() {
      return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
      );
    }
    

    为此,您必须等待 HTML 文档完全呈现(即,您的代理收到 webViewDidFinishLoad:)。

    P.S.:如果您想快速检查,也可以简单地尝试:

        NSUInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] intValue];
    

    这也应该可以,但上面的版本更便携。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多