OGeek|极客世界-中国程序员成长平台

标题: html - 使用 WkWebview 强制 HTML 内容适合设备宽度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:46
标题: html - 使用 WkWebview 强制 HTML 内容适合设备宽度

我有一个应该显示 HTML 电子邮件内容的 WkWebview。问题是有时内容比窗口大,因此需要用户滚动才能看到完整的电子邮件。

是否可以避免这种情况并具有与默认 iOS 邮件应用程序类似的行为?事实上,在邮件应用程序中,无论是哪种电子邮件,内容似乎都很合适。



Best Answer-推荐答案


您可以通过在网页加载后添加此代码来实现此目的。就是在这个函数末尾func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
    let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    let wkUController = WKUserContentController()
    wkUController.addUserScript(userScript)
    let wkWebConfig = WKWebViewConfiguration()
    wkWebConfig.userContentController = wkUController
    let yourWebView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig)// yourwebview is the webview that you are using.

关于html - 使用 WkWebview 强制 HTML 内容适合设备宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48905225/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4