UIWebView automatically resizes the font to best fit the page.

Is there a way to automatically resize the font in the UIWebView so that it looks best on the screen? Something that will try to set the font size to the ideal size for viewing (assuming that resizing the text simply wraps the text and does not spoil the entire page).

+4
source share
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 UIWebView with different widths. I want to enable scaling, so the scalePageToFit function is enabled, which results in tiny text for smaller UIWebviews and a larger font for larger UIWebviews. To standardize the font size, calculate the scale by dividing a certain factor (you have to adjust it yourself, 130,000 worked for me with a font size of 10pt CSS) according to the width of the webview.

As far as I can tell, it is not related to zoomFactor, which is always 1.0. I did not check if the fonts are exactly the same size (visually) in UIWebViews, but are good enough that I could not tell.

+7
source

You must set the scalesPageToFit property to YES

You can read the documentation here.

+2
source

All Articles