how to increase or decrease the UIWebview font size, not using scalePageToFit:NO;
In SWIFT -
let fontSize = 20
let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"
webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)
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.
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)%%'")
}
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];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize=@"143";
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
[myWebview stringByEvaluatingJavaScriptFromString:jsString];
}