Javascript 目标-C–;在可能修改文档内容后获取UIWebView动态size.height
我正在根据其中的内容动态调整我的Javascript 目标-C–;在可能修改文档内容后获取UIWebView动态size.height,javascript,objective-c,css,uiwebview,Javascript,Objective C,Css,Uiwebview,我正在根据其中的内容动态调整我的UIWebView。如果有大量内容,它可能跨越UIWebView的高度到1000px,如果内容较少,它可能跨越到500px(因此UIWebView将具有从0-n px的任意高度)。加载文档后,我用于查找UIWebView高度的代码如下: - (void)webViewDidFinishLoad:(UIWebView *)webView { // Change the height dynamically of the UIWebView to match
UIWebView
。如果有大量内容,它可能跨越UIWebView的高度到1000px,如果内容较少,它可能跨越到500px(因此UIWebView将具有从0-n px的任意高度)。加载文档后,我用于查找UIWebView高度的代码如下:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Change the height dynamically of the UIWebView to match the html content
CGRect webViewFrame = webView.frame;
webViewFrame.size.height = 1;
webView.frame = webViewFrame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
webViewFrame.size.width = 276; // Making sure that the webView doesn't get wider than 276 px
webView.frame = webViewFrame;
float webViewHeight = webView.frame.size.height;
}
这很好用。但为了增加复杂性,我正在使用javascript(jquery)检查文档中是否有宽度超过276像素(UIWebView的宽度)的图像
如果有一个更宽的图像,我会在img标签中添加一个名为“超大”的类,并使用css规则正确缩放图像,使其宽度不超过276像素。由于javascript在文档加载完成后运行,因此我无法再“捕捉”UIWebView高度运行javascript后如何获取新文档高度?
我使用的javascript如下所示:
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>
<![CDATA[
$(window).load(function(){
$(document).ready(function() {
$('img').each(function(e) {
if ($(this).width() > 276) {
$(this).parent().addClass('oversized');
}
});
});
});
//]]>
</script>
在-webViewDidFinishLoad:方法中,可以尝试添加
NSString *height = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
另外,不要使用jQuery来尝试查找过大的图像-在css中,只需将“宽度:100%”更改为“最大宽度:100%”,并为所有图像设置:
img {
border: 2px solid red;
max-width: 100%;
height: auto;
-webkit-box-sizing: border-box;
}
框大小调整规则将确保边框不会向宽度添加额外像素
img {
border: 2px solid red;
max-width: 100%;
height: auto;
-webkit-box-sizing: border-box;
}