使用WkWebview强制HTML内容适合设备宽度 [英] Force HTML content to fit in device width with WkWebview

查看:1100
本文介绍了使用WkWebview强制HTML内容适合设备宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WkWebview that is supposed to show the content of HTML emails. The issue is that sometimes the content is larger than the window, and so it requires the user to scroll in order to see the full email.

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

Is it possible to avoid this and have a similar behaviour as what happens with the default iOS mail app? In fact, in the Mail app, it seems that the content is fit, no matter the kind of email.

推荐答案

Swift

Similar to @nferocious76's but in Swift language

var scriptContent = "var meta = document.createElement('meta');"
scriptContent += "meta.name='viewport';"
scriptContent += "meta.content='width=device-width';"
scriptContent += "document.getElementsByTagName('head')[0].appendChild(meta);"

webView.evaluateJavaScript(scriptContent, completionHandler: nil)

Objective C

NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;

wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];

这篇关于使用WkWebview强制HTML内容适合设备宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆