最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]Swift中使用WKWebView显示出的网页中文字太小

Swift crifan 6090浏览 0评论
[背景]
折腾:
期间,使用WKWebView去显示了网页内容了,结果:
网页中文字太小了:
WKWebView text size too small
而对应的网页,在Safari中打开,字体大小是正常的:
same page on safari text ok
[折腾过程]
1.搜:
swift WKWebView text too small
参考:
然后去看看说的那个:
header block
2.搜:
WKWebView header block
参考:
看到一个:
WKUserContentController
搜到:
3.再搜:
WKWebView change header
WKWebView change html header
参考:
想要去试试:
let source = “document.body.style.background = \”#777\”;”
let userScript = WKUserScript(source: source, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController
self.webView = WKWebView(frame: self.view.bounds, configuration: configuration)
但是:
此处是想要设置html的值:
<meta name=”viewport” content=”initial-scale=1.0″ />
但是不会写对应的代码了:
不知道如何写这个script去设置对应的meta
这里也可以参考:
5.只能再去搞清楚:
6.先去试试代码:
        let curHeight = statusView.frame.height + tabbgView.frame.height
let webViewFrame = CGRectMake(0, curHeight, self.view.frame.size.width, self.view.frame.size.height – curHeight)

let jsSource = “document.body.style.background = \”#777\”;”
let userScript = WKUserScript(source: jsSource, injectionTime: .AtDocumentStart, forMainFrameOnly: true)

let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)

let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController

let webView = WKWebView(frame: webViewFrame, configuration: configuration)

let requestURL = NSURL(string: REGISTRATION_AGREEMENT_URL)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)

        self.view.addSubview(webView)
是否起效果:
结果是没有生效的,背景色还是白色,没变化.
-》
至少说明上述写法,此处没用。
再改为:
let userScript = WKUserScript(source: jsSource, injectionTime: .AtDocumentEnd, forMainFrameOnly:true)
结果:
起效果了:
 WKUserScript take changes
7.所以继续去测试:
        //let jsSource = “document.body.style.background = \”#777\”;”
let jsSource = “viewport = document.querySelector(\”meta[name=viewport]\”);” +
“viewport.setAttribute(‘content’, ‘initial-scale=1.0’);”

let userScript = WKUserScript(source: jsSource, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)

let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)

let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController

        let webView = WKWebView(frame: webViewFrame, configuration: configuration)
字体依旧很小。
8.再去试试:
        let jsSource = “var metaTag=document.createElement(‘meta’);” +
metaTag.name = \”viewport\”” +
“metaTag.content = \”width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\””+
        “document.getElementsByTagName(‘head’)[0].appendChild(metaTag);”
问题依旧。
9.再去换成:
        let jsSource = “var metaTag=document.createElement(‘meta’);” +
metaTag.name = \”viewport\”” +
//”metaTag.content = \”width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\”” +
“metaTag.content = \”initial-scale=1.0\””+
        “document.getElementsByTagName(‘head’)[0].appendChild(metaTag);”
结果:
问题依旧。
10.感觉好像是:
此处这部分的js,没有生效?
搜:
swift WKWebView javascript not working
参考:
11.然后突然想到:
去看看此处的url对应网页的源码
[已解决]Safari中如何查看网页源代码
然后看到的内容是:
<span style=”font-family:’Microsoft YaHei’, 微软雅黑, ‘Microsoft JhengHei’, 华文黑体, arial, sans-serif, STHeiti, sans-serif;font-size:medium;line-height:29px;background-color:#FFFFFF;”>用户。。。。。。的个人隐私信息。</span>
-》很明显,不是一个完整的网页:
-》不过之前的
document.body.style.background
的js的设置,还是有效果的。
-》而上述的meta的设置,还是没起到效果。
12.再去试试,把系数1.0加大试试:
“metaTag.content = ‘initial-scale=5.0′”+
结果:
问题依旧。
13.难道,或许,是iOS模拟器中,网页显示内容方面,有什么设置?
可以调整字体大小?
应该不是,但是也去摸索看看。
里面只有调整模拟器大小的:
ios emulator scale 75 percent
去看log,也没有什么有关于打开网页方面的异常:
emulator debug open system log no abnormal
找了半天,也没找到iOS模拟器打开网页相关方面的设置。
14.算了,暂时就这么样吧。
有空再继续折腾。

转载请注明:在路上 » [未解决]Swift中使用WKWebView显示出的网页中文字太小

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; // 自适应屏幕宽度js NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; // 添加自适应屏幕宽度js调用的方法 [wkUController addUserScript:wkUserScript]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebConfig]; [self.view addSubview:webView]; 添加JS来自适应屏幕,OC代码如上。
    夜锦凉7年前 (2016-12-08)回复
89 queries in 0.165 seconds, using 22.11MB memory