我的项目包中存储了一个 .html 文件。当我将其加载到 WebView.(UIWebview/WKWebview) 中时,数据已加载,但其中的表结构不可见。表格边框、列、行。这些值只是 float 的。在 Chrome 浏览器中可以正常打开。
let myURL = Bundle.main.url(forResource: "prescription", withExtension: "html")
let myURLRequest:URLRequest = URLRequest(url: myURL!)
webView.loadRequest(myURLRequest)
iOS 应用程序 UIWebView :
Html 页面是响应式的,可以适应任何尺寸,但我无法作为 Web 正确加载到 UIWebview 中。 我需要在本地存储该文件,因为我需要更改其值并在 webview 上显示。
最佳答案
找到解决方案: 如果您正在应用一些本地 css,其路径位于 HTML 文件中。 即。
- 将该 css 文件“myDefault.css”添加到您的 xcode 项目目录中。
- 将 HTML 转换为字符串,并将 HTML 文件中的 css 路径替换为本地路径。 :
使用基本 url 将 HTML 内容加载到 UIWebView。 (提供本地文件路径作为 baseUrl 很重要)
webView = UIWebView() let pathToInvoiceHTMLTemplate = Bundle.main.path(forResource: "presc", ofType: "html") let pathToHTMLCSS = Bundle.main.path(forResource: "myDefault", ofType: "css") do { var strHTMLContent = try String(contentsOfFile: pathToInvoiceHTMLTemplate!) strHTMLContent = strHTMLContent.replacingOccurrences(of: "./assets/stylesheets/myDefault.css", with: pathToHTMLCSS!) let pdfURL = URL(fileURLWithPath: pathToInvoiceHTMLTemplate!) webView.loadHTMLString(strHTMLContent, baseURL: pdfURL) }catch let err{ print(err) }
关于html - 在 Swift 中从本地文件编辑和加载 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51419865/