how to increase or decrease the UIWebview font size, not using scalePageToFit:NO;
11 Answers
I have 2 buttons - A- and A+
@interface
NSUInteger textFontSize;
- (IBAction)changeTextFontSize:(id)sender
{
switch ([sender tag]) {
case 1: // A-
textFontSize = (textFontSize > 50) ? textFontSize -5 : textFontSize;
break;
case 2: // A+
textFontSize = (textFontSize < 160) ? textFontSize +5 : textFontSize;
break;
}
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",
textFontSize];
[web stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
}

- 1,761
- 15
- 19
-
4Be sure to set `textFontSize` to an initial value (presumably 100) before calling this. – zekel Jun 18 '10 at 21:24
-
2It is also possible to set `...style.fontSize` (or other CSS properties). In my case this meant I didn't have to convert the scale back to a proper font size between sessions. – aerique Jan 13 '12 at 16:01
-
Seems broken in iOS10 on iPad only. Anyone have any fix or alternative? Thanks! – Chris Comeau Sep 09 '16 at 14:07
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize=@"143";
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
[myWebview stringByEvaluatingJavaScriptFromString:jsString];
}

- 21,652
- 10
- 63
- 102

- 167
- 1
- 4
There is currently no exposed way to directly manipulate the DOM in a UIWebView, nor any convenience methods for handling things like font sizes. I suggest filing Radars.
Having said that. you can modify the font size by changing the CSS, like any other webpage. If that is not possible (you don't control the content) you can write a small javascript function to change the CSS properties in the pages DOM, and execute it by calling:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

- 43,356
- 8
- 80
- 90
-
but if i change the CSS property through javascript, it lost the position where i was reading the content after increase/decrease the font. – Praveen-K Aug 10 '11 at 07:41
Swift 3
public func webViewDidFinishLoad(_ webView: UIWebView) {
let textSize: Int = 300
webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")
}
Older Swift:
func webViewDidFinishLoad(webView: UIWebView) {
let textSize: Int = 300
webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")
}

- 2,240
- 25
- 27
-
1While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – JAL Aug 27 '15 at 16:15
-
Swift 3 `webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")` – Ariel Gemilang Mar 22 '17 at 07:53
-
Swift 5 Update. This is an updated answer from @BLCs suggestion.
Also fixes the double %% in his string.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let textSize = 300
let javascript = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%'"
webView.evaluateJavaScript(javascript) { (response, error) in
print()
}
}

- 956
- 9
- 13
in my case im using a UISlider for the fontSize, can be any float value
func webViewDidFinishLoad(_ webView: UIWebView) {
if let fontSize: Float = (self.fontSizeSlider?.value) {
webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(fontSize)%%'")
print(fontSize)
}
}

- 555
- 4
- 16
Try the below code :
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",currentTextSize];
[_webview stringByEvaluatingJavaScriptFromString:setTextSizeRule];

- 12,720
- 7
- 52
- 72
Here is mine for changing font size and color in UIWebView:
NSString *myString=@"Hello World!";
int textFontSize=2;
NSString *myHTML=[NSString stringWithFormat: @"<html><body><script>var str = '%@'; document.write(str.fontsize(%d).fontcolor('green'));</script></body></html>",myString,textFontSize];
[myWebView loadHTMLString:myHTML baseURL:nil];

- 2,826
- 6
- 32
- 45
Try this simple method
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
int fontValue = 150;
NSString *webviewFontSize = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontValue];
[your_webview stringByEvaluatingJavaScriptFromString:webviewFontSize];
}

- 11,129
- 4
- 78
- 90
In SWIFT -
let fontSize = 20
let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"
webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)

- 13,571
- 6
- 76
- 98
Change the font size of the fetched HTML data by changing the occurrence of font size in the fetched html string to your required font size (40 in this example) in a method.
strHtml = [self htmlEntityDecode:strHtml];//parsing the html string
-(NSString *)htmlEntityDecode:(NSString *)string
{
string = [string stringByReplacingOccurrencesOfString:@"14px;" withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"15px;" withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"16px;" withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"17px;" withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"18px;" withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"19px;" withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"20px;" withString:@"40"];
return string;
}
So the html string: span style='font-size: 20px; Becomes: span style='font-size: 40
Note: Change the other occurrences like gt;, apos; too to get the desired string to load in your Webview.

- 1,106
- 13
- 15