How to de- and increase the font size with WKWebVi

2019-05-02 01:30发布

问题:

In Safari you can increase and decrease the font size. How can I achieve the same effect with WKWebView?

回答1:

The codes suggested by Inna used to work but stopped working from iOS10.3. I did some research and found that we need to make a slight change to it, just changing "%%" to a single "%".

So JS code should be:

let js = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='\(fontSize)%'"

I got this info from Apple Developer forum. https://forums.developer.apple.com/thread/51079



回答2:

Assume that you have fontUpdatedHeight integer which gives you the exact fontsize.

Just create string like that:

NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%lu%%'",(unsigned long)fontUpdatedHeight];

And execute the NSString

[self.WKWebView stringByEvaluatingJavaScriptFromString:jsString];