The font looks like smaller in WKWebView than in UIWebView

HtmlObjective CSwiftUiwebviewWkwebview

Html Problem Overview


I changed UIWebView to WKWebView, however, with the same html, the font in WKWebView looks like smaller than in UIWebView. I don't want this happen, so is there any way to avoid this change?

Html Solutions


Solution 1 - Html

Finally I solved this problem by adding an html string:

  • For Objective-C:
NSString *headerString = @"<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>";
[self.webView loadHTMLString:[headerString stringByAppendingString:yourHTMLString] baseURL:nil];
  • For Swift:
let headerString = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>"
webView.loadHTMLString(headerString + yourHTMLString, baseURL: nil)

What's more,if you want to load url rather than html you can try:

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 = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>"
        webView.loadHTMLString(headerString + (html as! String), baseURL: nil)
    }
    
}

Solution 2 - Html

let description = "<p> HTML content <p>"
var headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'></header>"
headerString.append(description)              
self.webView.loadHTMLString("\(headerString)", baseURL: nil)

Solution 3 - Html

Simple way to do this in Swift

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 = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header>"
        loadHTMLString(headerString + content, baseURL: baseURL)
    }
}

Just simply call this method and magic happen. ;)

webView.loadHTMLStringWithMagic(content: "<p> HTML content <p>", baseURL: nil)

OUTPUT: enter image description here

Solution 4 - Html

From iOS 14 onward you can achieve this with pageZoom property. For example

webView.pageZoom = 2.0;

will make page content twice as large.

Here's the link for documentation:

https://developer.apple.com/documentation/webkit/wkwebview/3516411-pagezoom

Solution 5 - Html

We can insert Headers into html throght javaScrip insertAdjacentHTML function in WKNavigationDelegate didFinish navigation method

extension HelpScreenViewController: WKNavigationDelegate {
// MARK: - WKNavigationDelegate
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    guard !isInjected else  {
        return
    }
    self.isInjected = true
    let js = """
             document.body.insertAdjacentHTML('beforebegin',"<header><meta 
             name='viewport' content='width=device-width, initial-scale=1.0, 
             maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'> 
             </header>")
             """
    webView.evaluateJavaScript(js) { (html, error) in
    }
    
}

}

Solution 6 - Html

This code work for me:

func fill(_ model:NewsModel){
    let description = "<p>\(model.details) <p>"
    var headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'></header>"
    headerString.append(description)
    self.detailsTestWebView.loadHTMLString("\(headerString)", baseURL: nil)
}

Solution 7 - Html

I fixed this by injecting the JavaScript code provided above in a neat way. Here is the way I instantiate WKWebView.

private func makeWebView() -> WKWebView {
    let config = WKWebViewConfiguration()
    let ctr = WKUserContentController()
    config.userContentController = ctr
    // JavaScript to inject
    let src = """
    document.body.insertAdjacentHTML('beforebegin',"<header><meta
    name='viewport' content='width=device-width, initial-scale=1.0,
    maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
    </header>");
    """
    let script = WKUserScript(source: src,
                              injectionTime: .atDocumentStart,
                              forMainFrameOnly: false)
    ctr.addUserScript(script)
    
    let webView = WKWebView(frame: .zero, configuration: config)
    webView.navigationDelegate = self
    webView.uiDelegate = self

    return webView
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question无夜之星辰View Question on Stackoverflow
Solution 1 - Html无夜之星辰View Answer on Stackoverflow
Solution 2 - Htmlishwar lal janwaView Answer on Stackoverflow
Solution 3 - HtmlIlesh PView Answer on Stackoverflow
Solution 4 - HtmlBerat CevikView Answer on Stackoverflow
Solution 5 - HtmlAmr AbdelWahabView Answer on Stackoverflow
Solution 6 - HtmlShahriar HossainView Answer on Stackoverflow
Solution 7 - HtmlBenjaminView Answer on Stackoverflow