私は UIWebView
の内容に応じて動的にサイズを変更しています。多くのコンテンツがある場合、UIWebViewの高さが1000pxに及ぶ可能性があります。コンテンツが少ない場合は、500pxになる可能性があります(UIWebViewは0〜npxの任意の高さを持ちます)。ドキュメントをロードした後に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;
}
これは完全に機能します。しかし、私は276ピクセル(私のUIWebViewの幅)より広い画像があるかどうかを確認するために、javascript(jquery)を使用している複雑に追加する。
IF there is an image that is wider I add a class to the img tag named "oversized" and have a css rule that will scale the image properly so it will not be wider than 276 pixels. Since the javascript runs after the document has finished loading I can not longer "catch" the UIWebView height. How can I get the new document height after the javascript has been run?
私が使用しているjavascriptは次のようになります。
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>
276) {
$(this).parent().addClass('oversized');
}
});
});
});
//]]>
</script>
そしてCSS:
.oversized * {
border: 2px solid red;
width: 100%;
height: auto;
}
あなたの-webViewDidFinishLoad:メソッドでは、
NSString *height = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
また、jQueryを使用して大判画像を検索する代わりに、 'width:100%'から 'max-width:100%'に変更してすべての画像に設定します。
img {
border: 2px solid red;
max-width: 100%;
height: auto;
-webkit-box-sizing: border-box;
}
ボックスサイズのルールは、枠線が幅に余分なピクセルを追加しないようにします。