UIWebView缩放页面使得不需要水平滚动 [英] UIWebView scaling page so that there is no need for horizontal scrolling
问题描述
有没有办法在UIWebView中缩放网页,以保持宽高比,但减少了水平滚动以阅读文章的需要。垂直滚动很好,我只是不希望用户不得不经常水平来回滚动来阅读文章的每一行。谢谢!
Is there a way to scale a web page in a UIWebView that maintains the aspect ratio but alleviates the need to scroll horizontally to read an article, for example. Vertical scrolling is fine, I just don't want the user to have to constantly be scrolling back and forth horizontally to read each line of the article. Thanks!
编辑:我用来创建视图的代码
the code I'm using to create the view
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, [[self view] frame].size.height)];
[_webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[_webView setDelegate:self];
[_webView setScalesPageToFit:YES];
[_webView loadRequest:[NSURLRequest requestWithURL:[headline link]]];
此链接始终已为手机格式化。
The link is always formatted for mobile already.
推荐答案
我认为为UIWebView定义视口可能有所帮助:
I think that defining the viewport for your UIWebView could help:
<meta name="viewport" content="width=device-width"/>
这是使用设备的全宽;如果你的UIWebView较小,你可以用点数指定它的宽度。
this is to use the full width of the device; if your UIWebView is smaller, that you can specify its width in points.
如果您需要在加载后将元标记添加到页面的HTML中,可以使用此代码:
In case you need to add the meta tag to the HTML of your page after loading it, you can use this code:
- (void) webViewDidFinishLoad:(UIWebView *)webView {
NSString* js =
@"var meta = document.createElement('meta'); "
"meta.setAttribute( 'name', 'viewport' ); "
"meta.setAttribute( 'content', 'width = device-width' ); "
"document.getElementsByTagName('head')[0].appendChild(meta)";
[webView stringByEvaluatingJavaScriptFromString: js];
}
同时检查这个SO线程为另一种技术。
Also check this S.O. thread for another technique for the same.
这篇关于UIWebView缩放页面使得不需要水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!