根据其内容调整 UIWebView 的大小

Resize UIWebView to its content

本文关键字:UIWebView 调整      更新时间:2023-09-26

在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方法提供了

- (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 的高度。不知道为什么(很想知道)。

所以我最终按照这个答案中的暗示做了:

- (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 的方式更优雅,但见鬼,它对我不起作用,也不知道为什么,而这种方法开箱即用。

与 Micheal 建议的类似,您应该能够通过在 Web 视图的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 则根据高度调整其Web视图的大小

我知道

这很旧,但我找不到我的问题的最新答案。我最终得到了这个解决方案。这在使用约束时有效。

- (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];
}

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

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

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

然后像对待任何其他视图一样对高度施展魔术。

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

也许缩短代码?

_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);