Bharathi
Bharathi

Reputation: 485

UIWebView zooming

I have set "scalesPageToFit" property to YES for UIWebView. Its getting zoom , but when the page loading, the content is with tiny font which cannot be read without pinching. Can we set the zooming scale as default?

Upvotes: 1

Views: 1807

Answers (1)

dom
dom

Reputation: 11962

This is more about the site you're loading then the UIWebView itself …

If you have controll over the displayed HTML just put <meta name="viewport" content="width=device-width;initial-scale=1.0"> inside your <head>.

If not: You can inject JavaScript into the UIWebView and execute it, so there is an easy way to insert the meta tag using a small code snippet:

NSString *result = [webView stringByEvaluatingJavaScriptFromString:
  [NSString stringWithFormat:@"var result = '';"
  "var viewport = null;"
  "var content = null;"
  "var document_head = document.getElementsByTagName( 'head' )[0];"
  "var child = document_head.firstChild;"
  "while ( child )"
  "{"
  " if ( null == viewport && child.nodeType == 1 && child.nodeName == 'META' && child.getAttribute( 'name' ) == 'viewport' )"
  "   {"
  "     viewport = child;"
  "     viewport.setAttribute( 'content' , 'width=device-width;initial-scale=1.0' );"
  "     result = 'fixed meta tag';"
  " }"
  " child = child.nextSibling;"
  "}"
  "if (null == viewport)"
  "{"
  " var meta = document.createElement( 'meta' );"
  " meta.setAttribute( 'name' , 'viewport' );"
  " meta.setAttribute( 'content' , 'width=device-width;initial-scale=1.0' );"
  " document_head.appendChild( meta );"
  " result = 'added meta tag';"
  "}"
  ]
];

Note, that the snippet will kill viewport settings made by the author of the HTML code … So you may get strange results.

Upvotes: 3

Related Questions