Posted By

tenveer on 01/06/11


Tagged

UIWebView


Versions (?)

Changing the range of the zoom factor for UIWebView objects


 / Published in: Objective C
 

  1.  
  2. 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.
  3.  
  4. 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.
  5.  
  6. The META tag can look like this:
  7.  
  8. <meta name="viewport" content="minimum-scale=0.6; maximum-scale=5; initial-scale=1; user-scalable=yes; width=640">
  9.  
  10. The following viewport parameters can be used:
  11.  
  12. * minimum-scale:
  13. This is the minimum zoom factor that is allowed. The default value is 0.25, the range is from >0 to 10.
  14. * maximum-scale:
  15. This is the maximum zoom factor that is allowed. The default value is 1.6, the range is from >0 to 10
  16. * initial-scale:
  17. 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.
  18. * user-scalable
  19. This parameter can be used to allow or disallow that the user can zoom the web page.
  20. * width:
  21. 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.
  22. * height:
  23. This parameter defines the height for the viewport. Usually this is calculated based on the width.
  24.  
  25. The JavaScript code could look like this:
  26.  
  27. File: IncreaseZoomFactor.js:
  28.  
  29. function increaseMaxZoomFactor() {
  30. var element = document.createElement('meta');
  31. element.name = "viewport";
  32. element.content = "maximum-scale=10";
  33. var head = document.getElementsByTagName('head')[0];
  34. head.appendChild(element);
  35. }
  36.  
  37. 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:
  38.  
  39. - (void)webViewDidFinishLoad:(UIWebView *)webView
  40. {
  41. NSString *path = [[NSBundle mainBundle] pathForResource:@"IncreaseZoomFactor" ofType:@"js"];
  42. NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  43. [webView stringByEvaluatingJavaScriptFromString:jsCode];
  44.  
  45. [webView stringByEvaluatingJavaScriptFromString:@"increaseMaxZoomFactor()"];
  46. }
  47.  
  48. 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.
  49.  
  50. 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).
  51.  

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: django on March 8, 2011

Hi I used your code and I noticed a little problem, while using. When I load a page, lets say just Google and zoom in my app crashes on a large zoom level. There's no meory warning or anything else, no bad excess nothing, it just crashes. First I thought it has to be something else within the app, but I made a clear project and as soon as there is another view I get these problems. Did anyone else ever had something like this?

Thanks for any help!

You need to login to post a comment.