Ik ben bezig met het dynamisch wijzigen van de grootte van mijn UIWebView
, afhankelijk van de inhoud. Als er veel inhoud is, kan dit de hoogte van de UIWebView tot 1000 px omspannen en als de inhoud minder is, kan de breedte oplopen tot 500px (dus de UIWebView heeft een willekeurige hoogte van 0-n px). De code die ik gebruik om de UIWebView-hoogte te achterhalen nadat het document is geladen, is dit:
- (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;
}
Dit werkt perfect. Maar om toe te voegen aan de complexiteit, gebruik ik JavaScript (jQuery) om te controleren of er een afbeelding in het document is die breder is dan 276 pixels (de breedte van mijn UIWebView).
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?
Het JavaScript dat ik gebruik ziet er zo uit:
<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>
En de CSS:
.oversized * {
border: 2px solid red;
width: 100%;
height: auto;
}