开发者

Setting UIWebView width & height

I need adjust the height and width of UIWebView so that the whole text is displayed within the view and the user开发者_如何学运维 need not scroll to read the text.

Does anyone has any idea how to get it?


webView.scalesPageToFit = YES;

See also: UIWebView+SFHFStringMetrics


Hopefully this will point you in the right direction.

By calling the stringByEvaluatingJavaScriptFromString: method on the UIWebView you can execute JavaScript from your application in the UIWebView.

// get the height of your content
[webview stringByEvaluatingJavaScriptFromString:@"document.getElementById('id_of_content_container').offsetHeight;"];

// get the width of your content
[webview stringByEvaluatingJavaScriptFromString:@"document.getElementById('id_of_content_container').offsetHeight;"];

The above will return an NSString object with the width and height of whatever element you pass into the getElementById function. With that information you should be able to resize the UIWebView to accommodate your content. An example:

CGRect frame = webView.frame;
frame.size.width = 100;
webView.frame = frame;

You may also need to reset scalesPageToFit again to get the content to readjust to the new frame size.

Another option would be to detect window.height and window.width on the HTML side and use JavaScript to adjust the content to fit within the aspects of the UIWebView.


There are no special provisions on UIWebView for resizing to fit the content. You could set the scalesPageToFit property to YES to scale the content down, or you could make the view large enough to fit most reasonably sized pages. I infer from your question that you're displaying content you're in control of. If not, I'm not sure there's an easy programmatic way to achieve what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜