WKWebView作為“新來”的則必然代表它有些“特殊”,加載出來的文字大小與在瀏覽器選擇手機(jī)模式時(shí)的不一致。解決方法如下:
1.讓前端小哥哥,小姐姐幫忙修改原HTML文件。
2.自己處理,利用WKWebView向網(wǎng)頁(yè)內(nèi)容中注入JS代碼 (經(jīng)測(cè)試有效果)
- (WKWebView *)webView {
if (!_webView) {
//以下代碼適配大小
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;
_webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];
[self.view addSubview:_webView];
}
return _webView;
}