javascript 将 UIWebView 调整为其内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6105294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resize UIWebView to its content
提问by ferostar
In a UITextView i can resize the height of the control to it's content like this:
在 UITextView 中,我可以将控件的高度调整为它的内容,如下所示:
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
But i want to use an UIWebView instead in order to show rich text. The UIWebVie's content is:
但我想改用 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];
So the UIWebView contains nothing but plain text. How can i achieve result similar to the above UITextView code?
所以 UIWebView 只包含纯文本。我怎样才能获得类似于上面 UITextView 代码的结果?
回答by ferostar
Although in theory the JavaScript method provided
虽然理论上 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)];
}
should work (and seems to work for most people), it just didn't for me. I tried with a number of variation and it always returned a size that has nothing to do with the true content's height. A one liner would come up with 260 or 244 height, for instance. Don't know why (would love to know).
应该有效(并且似乎对大多数人有效),但对我来说却没有。我尝试了许多变体,但它总是返回与真实内容高度无关的大小。例如,一个班轮会提出 260 或 244 的高度。不知道为什么(很想知道)。
SO i ended up doing as hinted in thisanswer:
所以我最终按照这个答案中的提示做了:
- (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);
}
I think the JS way more elegant but hell, it doesn't works for me and can't figure out why, while this method worked out of the box.
我认为 JS 的方式更优雅但地狱,它对我不起作用并且无法弄清楚原因,而这种方法开箱即用。
回答by netpro2k
Similar to what Micheal suggested, you should be able to get the height of the content by making a call to javascript in the webview's webViewDidFinishLoad
callback
与 Micheal 建议的类似,您应该能够通过在 webview 的webViewDidFinishLoad
回调中调用 javascript 来获取内容的高度
ex (untested):
前(未经测试):
- (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)];
}
This is based on a method used in Appcelerator Titanium to resize its webviews based on height if the user sets height to "auto" https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TiUIWebView.m#L616
这是基于 Appcelerator Titanium 中使用的一种方法,如果用户将高度设置为“自动” https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TiUIWebView.m#,它会根据高度调整其 webview 的大小L616
回答by speedrock
I know this is old but I could not find any more recent answers to my problem. I ended up with this sollution. This is valid when using constraints.
我知道这已经过时了,但我找不到任何关于我的问题的最新答案。我最终得到了这个解决方案。这在使用约束时有效。
- (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];
}
回答by Priyanka
Is this what you are looking for ?? UIScrollView *scroll= [[webView subviews]objectAtIndex:0]; CGFloat flo= scroll.contentSize.height;
这是你想要的 ??UIScrollView *scroll= [[webView subviews]objectAtIndex:0]; CGFloat flo= scroll.contentSize.height;
回答by Michael Petrov
There is no simple answer to this, as a UIWebView is a complex scroll view. A few things you could try, all must be done after the UIWebView delegate gets notified that the webpage finished loading (using webViewDidFinishLoad:). Once the delegate knows that UIWebView is loaded, I would try this:
对此没有简单的答案,因为 UIWebView 是一个复杂的滚动视图。您可以尝试一些事情,所有这些都必须在 UIWebView 委托收到网页完成加载的通知后完成(使用 webViewDidFinishLoad:)。一旦委托知道 UIWebView 已加载,我会尝试这样做:
- Try the same contentSize trick, unlikely that it will work, but it might if the page is loaded
Get the height using javascript, like this:
CGFloat windowHeight = [[webView stringByEvaluatingJavaScriptFromString:@"return document.body.scrollHeight;"] floatValue];
- 尝试相同的 contentSize 技巧,不太可能奏效,但如果页面已加载,则可能奏效
使用 javascript 获取高度,如下所示:
CGFloat windowHeight = [[webView stringByEvaluatingJavaScriptFromString:@"return document.body.scrollHeight;"] floatValue];
Then do your magic with the height as you would with any other view.
然后像处理任何其他视图一样对高度施展魔法。
回答by chikuba
Maybe shorten the code?
也许缩短代码?
_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);