how to increase font size in UIWebView
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];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize=@"143";
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
[myWebview stringByEvaluatingJavaScriptFromString:jsString];
}
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;
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)%%'")
}