我已经尝试了堆栈溢出的所有可用解决方案
任何解决办法都帮不上忙

extension ViewController: WKNavigationDelegate,WKUIDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.insertContentsOfCSSFile2(into: webView)
        self.insertContentsOfCSSFile1(into: webView)
    }

    func insertContentsOfCSSFile2(into webView: WKWebView) {
        guard let path = Bundle.main.path(forResource: "article-style", ofType: "css") else { return }
        let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
        let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
        print(jsString)
        webView.evaluateJavaScript(jsString) {(result, error) in
            if let error = error {
                print(error)
            }
        }
    }

    func insertContentsOfCSSFile1(into webView: WKWebView) {
        guard let path = Bundle.main.path(forResource: "custom", ofType: "css") else { return }
        let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
        let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
        print(jsString)
        webView.evaluateJavaScript(jsString) { (result, error) in
            if let error = error {
                print(error)
            }
            if let result = result {
                print(result)
            }
        }
    }
}

这里是web视图初始化
func loadUrl() {

    webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
    webView.navigationDelegate = self
    webView.allowsLinkPreview = true
    webView.uiDelegate = self
    webViewContainer.addSubview(webView)
    self.addConstraints(to: webView, with: webViewContainer)

    do {
        guard let filePath = Bundle.main.path(forResource: "article", ofType: "html")
            else {
                print ("FILE READING ERROR")
                return
        }
        let contents =  try String(contentsOfFile: filePath, encoding: .utf8)
        let baseUrl = URL(fileURLWithPath: filePath)
        print(baseUrl)
        print(contents)
        webView.loadHTMLString(htmlString, baseURL:URL(fileURLWithPath: Bundle.main.bundlePath))
    }
    catch {
        print ("FILE HTML ERROR")
    }
}

“article.html”、“article style.css”和“custom.css”是我的项目目录中的三个本地文件
我从viewDidLoad()方法调用webView加载请求
没有什么是我没有尝试过却解决不了的
任何帮助都会受到极大的重视

最佳答案

必须将HTML标记文件添加到项目中。并在代码中引用此文件时使用rickste。像这样的东西
let filePath = Bundle.main.path(forResource: resource, ofType: "html", inDirectory: "www") var htmlString = try? String(contentsOfFile: filePath ?? "", encoding: .utf8) htmlString = htmlString?.replacingOccurrences(of: "TABLEVALUE", with: html)loadHTMLString(html, baseURL: nil)
根据您的要求,编辑这样的文件。

<head>

<style type="text/css">
    table{
        width: 100%;
        overflow-x: scroll;
    }

td{
    font-size: 14px;
    font-family:"LucidaSansOT";
    padding: 15px;
    border: white 0.5px solid
}

tr{
    background: #fbdfb2;
}

tr:first-child{
    background: #f29400;
}
window.onload = function() {
    window.location.href = "ready://" + document.body.offsetHeight;
}
</style>
</head>
<body>
    <div>TABLEVALUE</div>
</body>

例子
https://prntscr.com/ltwbg7

关于swift - 如何在wkWebView Swift中将本地CSS应用于HTML内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53738606/

10-09 16:20