Search code examples
ioscssswiftuiwebview

style tag is ignored by uiwebview


I'm trying to give my html an css inline style. But this style gets ignored.

This is the HTML string that I'm using for webView.loadHTMLString(htmlBelow, baseURL: Bundle.main.bundleURL)

let fontsize = 16 //This is an dynamic variable

<!DOCTYPE html>
<html lang=\"en\">
    <head>
        <meta charset=\"UTF-8\">
        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
        <meta http-equiv=\"X-UA-Compatible\"content=\"ie=edge\">
        <title>Document</title>
        <style>
            ​html {
                font-size:\(fontsize)px; */THIS DOESN'T WORK/*
            }
        </style>
        <link rel=\"stylesheet\" href=\"\style.css\">
        <script src=\"jquery-3.4.1.min.js\"></script>
        <script src=\"script.js\"></script>
    </head>
    <body>
        <div class=\ "sqr-tree-level\"> \(restOfHtml) </div>
    </body>
</html>

I'm trying to set the fontsize dynamically. When I change the font-size in my css file it works, but I want to set it dynamically. That's why I wanna do it like this.

This is how my document.head.innerHTML looks like


Solution

  • For future reference the fix here was to put the styling in to the <body> tag instead of in the head <style> tag.

    <body style=\"font-size:\(fontsize)px;\">