Is there a way to resize font automatically in a UIWebView so it will look best on screen? Something that will try to set the font size to be in the ideal size for viewing (assuming resizing the text will just wrap the text and not corrupt the whole page).
-
Accept the correct answer please. – vrwim Mar 09 '15 at 16:24
2 Answers
I had the same problem, this workaround worked for me:
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[webView scalesPageToFit];
float scale = 130000/webView.scrollView.frame.size.width;
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'", scale];
[webView stringByEvaluatingJavaScriptFromString:jsString];
}
I have several UIWebViews of different widths. I want to keep zooming enabled, so scalePageToFit is switched on, which results in tiny text for the UIWebviews of smaller width and large font for UIWebviews of a larger width. To standardise the font size, calculate the scale by dividing some factor (you'll have to adjust this yourself, 130000 worked for me with CSS font size 10pt) by the webview width.
As far as I can tell it's not related to zoomFactor, which is always 1.0. I haven't checked if the fonts are exactly the same size (visually) across the UIWebViews, but good enough for me not to be able to tell.

- 34,814
- 22
- 96
- 117

- 4,136
- 3
- 35
- 55
-
-
It'll depend on the size of your input font - tweak the 130000 number. – Johnny Rockex Nov 10 '16 at 09:28
You should set the scalesPageToFit
property to YES
You can read the documentation Here

- 2,198
- 1
- 17
- 23
-
Thanks. I know about "scalesPageToFit". The problem is, that when I use it, the font looks very small. So I would like to automatically increase it's size until the font is large enough for viewing. – bashan Dec 08 '12 at 20:49
-
if `scalesPageToFit` is set to YES, then the user should be able to zoom the UIWebView to their liking. – Adam Johnson Dec 08 '12 at 20:53
-
2I am trying to create experience in which the page is loaded and the text size best fits (in terms of font size) without user having to resize it. – bashan Dec 08 '12 at 20:58
-
The optimal page size is pretty subjective. If you know the specific page you are loading, and wish to set it to a particular zoom level, you could access the `scrollView` property of the `UIWebView` and set the `zoomScale` -- something like `self.webView.scrollView.zoomScale = 1.5f;` – Adam Johnson Dec 08 '12 at 21:03
-
1The zoom scale is not good, because the quality of the font dramatically reduced. In addition, it is not the scale that I want to enlarge, it is the text in the page itself. Regarding "optimal page size": I believe there is a font size which can be considered "good for reading" even if not optimal for this user or another. Currently the font in some pages looks way to small for reading. If I enlarge it manually (by code) it looks OK. But I would like enlargement to be somehow calculated automatically. – bashan Dec 09 '12 at 15:04