Demo entry 6383683

111

   

Submitted by anonymous on May 23, 2017 at 18:54
Language: Swift. Code size: 1.1 kB.

// 1.创建一个可变字符串用来拼接js代码
        let newsBody:NSMutableString = NSMutableString()
        // 2.拼接html、head、body
            // 2.1拼接html
        newsBody.append("<html>")
            // 2.2拼接head(大多数css,js都写在head里面)
        newsBody.append("<head>")
            // 导入外部css,作用就是修饰网页
        
        let cssPath = Bundle.main.url(forResource: "News.css", withExtension: nil)
        
        newsBody.appendFormat("<link  rel=\"stylesheet\" href=%@>", cssPath! as CVarArg)
        
        newsBody.append("</head>")
        
            // 2.3拼接body
        newsBody.append("<body>")
        
        
                // 2.3.1拼接标题
        newsBody.appendFormat("<div class = \"title\">  %@</div>", title)
                // 2.3.2拼接时间和来源
        newsBody.appendFormat("<div class = \"time\"> %@ %@</div>", ptime,source)
                // 2.3.3拼接数据
//        newsBody.append(body)
        
        newsBody.append(appendNewsImages(htmlString: body))
        print(newsBody)
        newsBody.append("</body>")
        
        newsBody.append("</html>")
        webView.loadHTMLString(newsBody as String, baseURL: nil)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).