菜鸟教程小白 发表于 2022-12-11 19:46:54

html - 使用 WkWebview 强制 HTML 内容适合设备宽度


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

<p>是否可以避免这种情况并具有与默认 iOS 邮件应用程序类似的行为?事实上,在邮件应用程序中,无论是哪种电子邮件,内容似乎都很合适。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以通过在网页加载后添加此代码来实现此目的。就是在这个函数末尾func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) </p>

<pre><code>let jscript = &#34;var meta = document.createElement(&#39;meta&#39;); meta.setAttribute(&#39;name&#39;, &#39;viewport&#39;); meta.setAttribute(&#39;content&#39;, &#39;width=device-width&#39;); document.getElementsByTagName(&#39;head&#39;).appendChild(meta);&#34;
    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.
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于html - 使用 WkWebview 强制 HTML 内容适合设备宽度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48905225/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48905225/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: html - 使用 WkWebview 强制 HTML 内容适合设备宽度