XZ_iOS 之 WKWebView字体font相比UIWebView更小

我将UIWebView更改为WKWebView,使用相同的html,WKWebView中的字体看起来比UIWebView中的字体小。有什么办法可以避免这种更改?

 

我是通过添加一个html字符串解决了这个问题:

对于Objective-C:

NSString *headerString = @"
"; [self.webView loadHTMLString:[headerString stringByAppendingString:yourHTMLString] baseURL:nil];
对于Swift:
方法一:
let headerString = "
" webView.loadHTMLString(headerString + yourHTMLString, baseURL: nil) 方法二: extension WKWebView { /// load HTML String same font like the UIWebview /// //// - Parameters: /// - content: HTML content which we need to load in the webview. /// - baseURL: Content base url. It is optional. func loadHTMLStringWithMagic(content:String,baseURL:URL?){ let headerString = "
" loadHTMLString(headerString + content, baseURL: baseURL) } } 调用: webView.loadHTMLStringWithMagic(content: "

HTML content

", baseURL: nil)

此外,如果您要加载url而不是html,则可以尝试:

private var isInjected: Bool = false
webView.navigationDelegate = self
// MARK: - WKNavigationDelegate
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    if isInjected == true {
        return
    }
    self.isInjected = true
    // get HTML text
    let js = "document.body.outerHTML"
    webView.evaluateJavaScript(js) { (html, error) in
        let headerString = "
" webView.loadHTMLString(headerString + (html as! String), baseURL: nil) } }


 

你可能感兴趣的:(iOS,OC,Swift,WKWebView字体小,UIWebView改为WK)