Nit*_*itk 46
你可以以webView.scalesPageToFit=YES;编程方式使用
如果你在xib中使用而不仅仅是 click the check box "Scaling" scales Page to fit
iPa*_*tel 28
这个逻辑用于缩放UIWebView,无需在UIScrollView上添加UIWebView
唯一的问题webView.scalesPageToFit = YES;是,它会改变字体大小的初始内容,但我找到了其他选项
添加<UIWebViewDelegate, UIScrollViewDelegate>到您的.h文件
创造你的 UIWebView.
self.mWebview = [[UIWebView alloc] init];
self.mWebview.delegate = self; /// set delegate method of UIWebView
self.mWebview.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 80); // set frame whatever you want..
[self.mWebview setOpaque:NO];
self.mWebview.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.mWebview];
Run Code Online (Sandbox Code Playgroud)
加载HTML文件/内容.
NSString* htmlString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File Name"ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
[self.mWebview loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
#pragma mark -
#pragma mark - Webview Delegate Methods
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.scrollView.delegate = self; // set delegate method of UISrollView
webView.scrollView.maximumZoomScale = 20; // set as you want.
webView.scrollView.minimumZoomScale = 1; // set as you want.
//// Below two line is for iOS 6, If your app only supported iOS 7 then no need to write this.
webView.scrollView.zoomScale = 2;
webView.scrollView.zoomScale = 1;
}
#pragma mark -
#pragma mark - UIScrollView Delegate Methods
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
self.mWebview.scrollView.maximumZoomScale = 20; // set similar to previous.
}
Run Code Online (Sandbox Code Playgroud)
注意:我必须使用Xcode 5.1.1和iOS 6.1及更高版本在Mac OS X - 10.9.3上进行测试.
我希望这对你有所帮助.:)
Ash*_*shu 17
如果你想通过编程方式做,那么使用
webView.scalesPageToFit = true;
Run Code Online (Sandbox Code Playgroud)
如果您正在使用故事板,则必须勾选"缩放"复选框以缩放页面以适合
