0

I have the following code

@IBOutlet weak var webView: WKWebView!
if var body = self.data.body {
    body += "<meta name=\"viewport\" content=\"initial-scale=1.0\" />"
    webView.loadHTMLString(body, baseURL: nil)

    let css = "body { font-size: 18px; line-height: 28px; } h1 { font-size: 40px; text-align: left; margin-bottom: 12px; line-height: 1.3;  } h2 { font-size: 26px; font-weight: 700; padding: 0; margin-bottom: 10px; text-align: left; line-height: 34.5px; letter-spacing: -0.45px; } p, i, a { margin-top: 21px; font-size: 20px; letter-spacing: -0.03px; line-height: 1.57; }";

    let js = "var style = document.createElement('style'); style.innerHTML = '\(css)'; document.head.appendChild(style);"

    webView.evaluateJavaScript(js, completionHandler: nil)
}

Unfortunately, the webview doesn't show the right font size. Also, how do I change the font family of the body? I'm I doing something wrong?

Rue Vitale
  • 1,549
  • 4
  • 17
  • 24
  • https://stackoverflow.com/a/33126467/10584330 Try this one.Same problem i solved it. – Anjali Shah Feb 17 '20 at 04:52
  • on a personal opinion i find it always more user friendly to manually load a HTML file and CSS file externally to the WebView instead of adding the CSS here like you have – Anjula Serasinghe Feb 17 '20 at 05:00
  • Does this answer your question? [Insert CSS into loaded HTML in UIWebView / WKWebView](https://stackoverflow.com/questions/33123093/insert-css-into-loaded-html-in-uiwebview-wkwebview) – Mohit Kumar Feb 17 '20 at 07:15

0 Answers0