【问题标题】:Resize UIWebView to its content将 UIWebView 调整为其内容
【发布时间】:2011-08-31 14:36:47
【问题描述】:

在 UITextView 中,我可以将控件的高度调整为它的内容,如下所示:

CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;

但我想改用 UIWebView 来显示富文本。 UIWebVie 的内容是:

NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                  "<head> \n"
                                  "<style type=\"text/css\"> \n"
                                  "body {font-family: \"%@\"; font-size: %@; }\n"
                                  "</style> \n"
                                  "</head> \n"
                                  "<body>%@</body> \n"
                                  "</html>", @"helvetica",
                                  [NSNumber numberWithInt:12], @"yadayadayada..."];
[webView loadHTMLString:myDescriptionHTML baseURL:nil];

所以 UIWebView 只包含纯文本。我怎样才能达到类似于上面的 UITextView 代码的结果?

【问题讨论】:

标签: javascript iphone objective-c uiwebview


【解决方案1】:

我知道这是旧的,但我找不到任何关于我的问题的最新答案。 我最终得到了这个解决方案。这在使用约束时有效。

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    // get content size
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    // ajust the height constraint of the webview to the content size
    [self.webviewHeightConstraint setConstant:fittingSize.height];
}

【讨论】:

    【解决方案2】:

    也许可以缩短代码?

    _webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, 1);
    CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
    _webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, fittingSize.height);
    

    【讨论】:

      【解决方案3】:

      虽然理论上提供了 JavaScript 方法

      - (void)webViewDidFinishLoad:(UIWebView *)webview
          CGRect oldBounds = [[self webview] bounds];
          CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
          [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)];
      }
      

      应该有效(并且似乎对大多数人有效),但对我来说却没有。我尝试了多种变化,它总是返回一个与真实内容高度无关的大小。例如,一个衬里会提出 260 或 244 的高度。不知道为什么(很想知道)。

      所以我最终按照this 回答中的提示做了:

      - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
          CGRect frame = aWebView.frame;
          frame.size.height = 1;
          aWebView.frame = frame;
          CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
          frame.size = fittingSize;
          aWebView.frame = frame;
      
          NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
      }
      

      我认为 JS 的方式更优雅,但是地狱,它对我不起作用并且无法弄清楚为什么,而这种方法是开箱即用的。

      【讨论】:

        【解决方案4】:

        这就是你要找的吗?? UIScrollView *scroll= [[webView 子视图]objectAtIndex:0]; CGFloat flo= scroll.contentSize.height;

        【讨论】:

          【解决方案5】:

          类似于 Micheal 的建议,您应该能够通过在 webview 的 webViewDidFinishLoad 回调中调用 javascript 来获取内容的高度

          ex(未经测试):

          - (void)webViewDidFinishLoad:(UIWebView *)webview
              CGRect oldBounds = [[self webview] bounds];
              CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
              [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)];
          }
          

          这是基于 Appcelerator Titanium 中使用的一种方法,如果用户将高度设置为“自动”https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TiUIWebView.m#L616

          【讨论】:

          • 如果你第二次调用 loadHTMLString:baseURL: 这将使你的 UIWebView 变大,HTML 页面较长,但 HTML 页面较短时不会变小。在加载新的 HTML 字符串时,对 JavaScript 使用“document.body.clientHeight”来缩小和增加 UIWebView 的高度。
          【解决方案6】:

          对此没有简单的答案,因为 UIWebView 是一个复杂的滚动视图。您可以尝试一些事情,所有事情都必须在 UIWebView 委托收到网页完成加载的通知后完成(使用 webViewDidFinishLoad:)。一旦代理知道 UIWebView 已加载,我会尝试以下操作:

          • 尝试使用相同的 contentSize 技巧,它不太可能会起作用,但如果页面已加载,它可能会起作用
          • 使用 javascript 获取高度,如下所示:

            CGFloat windowHeight = [[webView stringByEvaluatingJavaScriptFromString:@"return document.body.scrollHeight;"] floatValue];

          然后像处理任何其他视图一样对高度施展魔法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-26
            • 1970-01-01
            • 2014-03-16
            • 2011-08-20
            • 2012-01-08
            • 2023-03-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多