問題:用WKWebView加載 html 富文本
self.webView.loadHTMLString(msgStr, baseURL: nil)
如果對應(yīng)的 html 富文本沒有標(biāo)注字體大小,只使用<p>、<span>、<h>等標(biāo)簽,此時在手機(jī)上面顯示的字體會比在電腦網(wǎng)頁上面的看起來小很多,影響閱讀
解決方案
1、設(shè)置WKWebView代理
class MessageDetailsViewController: BaseViewController, WKNavigationDelegate
webView = WKWebView()
webView.frame = CGRect(x: 0, y: kTopHeight, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - kTopHeight)
webView.navigationDelegate = self
self.view.addSubview(webView)
2、實(shí)現(xiàn)WKWebView代理,在WKWebView加載結(jié)束后,使用JavaScript代碼來更改WebView中的字體大小
// WKNavigationDelegate 方法
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
printLog("Finished loading")
let script = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(200)%'"
webView.evaluateJavaScript(script, completionHandler: nil)
}
通過修改上面的百分比,就可以實(shí)現(xiàn)動態(tài)的修改html 富文本在手機(jī)上顯示的字體大小了