htmliosswiftswiftsoup

Dynamically create h1 tags using swift


My question is how can I dynamically create h1 tags using Swift and add it to the front of the HTML file.

This is what I have and it is not working.

extension WKWebView {

func loadHTML(html: String) {
    let htmlString = """
    <link rel="stylesheet" type="text/css" href="style.css">
    <h1> + String(htmlEncodedString:post.title) + </h1>
    <meta name="viewport" content="initial-scale=1.0" />
    <span>\(html)</span>
    """
    self.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
} }

and this is the output " + String(htmlEncodedString:post.title) + "


Solution

  • Use interpolation:

    let htmlString = """
    <link rel="stylesheet" type="text/css" href="style.css">
    <h1> + \(String(htmlEncodedString:post.title)) + </h1>
    <meta name="viewport" content="initial-scale=1.0" />
    <span>\(html)</span>
    """