Return to Snippet

Revision: 38745
at January 6, 2011 22:10 by tenveer


Initial Code
When using a UIWebView to display a web page, you have probably noticed that the maximum zoom factor is very limited. In mobile Safari you can zoom in much farther than in your own App. This blog post will explain, how you can increase the maximum zoom factor.

The UIWebView class does not have any methods to set the zoom factor. But this can be done with certain instructions within the HTML code. The META tag can be used to configure the viewport, which includes the initial, the minimum and the maximum zoom factor.

The META tag can look like this:

<meta name="viewport" content="minimum-scale=0.6; maximum-scale=5;  initial-scale=1; user-scalable=yes; width=640">

The following viewport parameters can be used:

    * minimum-scale:
      This is the minimum zoom factor that is allowed. The default value is 0.25, the range is from >0 to 10.
    * maximum-scale:
      This is the maximum zoom factor that is allowed. The default value is 1.6, the range is from >0 to 10
    * initial-scale:
      This is the zoom factor that is used when the web page is loaded before the user zooms in or out. The default value is calculated so that the web page fits in the visible area. But the final range will be also within the range from the minimum to the maximum factor.
    * user-scalable
      This parameter can be used to allow or disallow that the user can zoom the web page.
    * width:
      This parameter defines the width for the viewport. By default the width is set to 980 px on an iPhone. The possible range for this value is from 200 to 10000. The special value “device-width” represents the width of the device (which is 320 on an iPhone and 768 on an iPad). Please note that the device width is not the same as the width of the user interface. The device width is always represented by the width of the device in portrait orientation (the “natural” orientation of the device). If we want to increase the maximum zoom factor of a web page (the default factor is 1.6), we only need to add a META tag in the HTML code of the page, which defines the maximum zoom factor. If you can change the original HTML code, you can simply add the META tag and you’re done. If your App loads web pages from the internet and you can’t change the original HTML code, you have to write JavaScript code which creates a META tag and adds it to the HTML code of the web page. If you’ve read my other blog posts, you’ll already know how this works.
    * height:
      This parameter defines the height for the viewport. Usually this is calculated based on the width.

The JavaScript code could look like this:

File: IncreaseZoomFactor.js:

function increaseMaxZoomFactor() {
  var element = document.createElement('meta');
  element.name = "viewport";
  element.content = "maximum-scale=10";
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(element);
}

Within the webViewDidFinishLoad: delegate method of the UIWebView object you can then inject this JavaScript code into the web page and call the function to increase the maximum zoom factor:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  NSString *path = [[NSBundle mainBundle] pathForResource:@"IncreaseZoomFactor" ofType:@"js"];
  NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  [webView stringByEvaluatingJavaScriptFromString:jsCode];

  [webView stringByEvaluatingJavaScriptFromString:@"increaseMaxZoomFactor()"];
}

When dealing with web pages from the web, you have to be careful because many of these web pages do already use META tags to change the zoom factor or other viewport parameters. If you add the new META tag as described above, you’ll overwrite the maximum-scale parameter and the other parameters remain unchanged. Which means, if the same parameter is defined in multiple META tags, the last one wins. Most of the time this is fine, but in some cases this can have some side effects.

For example if the web page defines some parameters which would result in an initial zoom factor of 4, but because the page does not define the maximum-scale parameter, the default value of 1.6 would also limit the initial zoom factor to 1.6. If you now increase the maximum-scale parameter, the initial zoom factor will be increased as well because it is no longer limited by the maximum zoom factor. If this can be critical for your App, you need to check for the existing parameters and based on their values you may need to define some other parameters as well (for example you may explicitly add the initial-scale parameter with a value of 1.6 to prevent that the maximum zoom factor initially zooms the page much more than it would do without your modification).

Initial URL

                                

Initial Description

                                

Initial Title
Changing the range of the zoom factor for UIWebView objects 

Initial Tags

                                

Initial Language
Objective C