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];