1:由于直接加载的文字变小,或者图片大小超过屏幕会不适配,则以下代码可以完美适配, 加入标签头
//加标签头适配
let headStr = "<head><p align=\"center\" style=\"font-size:14px; color:#333333\">\(titleStr)</p><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"><style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;}</style></head>"
let contentStr = "<html>" + headStr + self.htmlStr + "</html>"
self.h5WebView.loadHTMLString(contentStr, baseURL: nil)
2:遵循的代理,不想麻烦直接粘了可以用,记得改一下了类名(禁止用户选择还是不错的,有需求用得着)
extension JKNoticeDetailVC: WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
self.showLoadingView("")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.hiddenLoadingView()
//禁止用户选择
webView.evaluateJavaScript("document.documentElement.style.webkitUserSelect='none';")
webView.evaluateJavaScript("document.activeElement.blur();")
//文本文字的大小百分比
// webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'")
}
//提交发生错误时调用
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
self.hiddenLoadingView()
}
// 页面加载失败时调用
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
self.hiddenLoadingView()
}
//执行这个代理 didFailProvisionalNavigation url错误时才会执行
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
}