美文网首页
iOS 使用wkwebview 加载HTML5,字体内容变小?

iOS 使用wkwebview 加载HTML5,字体内容变小?

作者: iOS开发小学生 | 来源:发表于2021-08-17 10:36 被阅读0次

WKWebView作为“新来”的则必然代表它有些“特殊”,加载出来的文字大小与在浏览器选择手机模式时的不一致。解决方法如下:
1.让前端小哥哥,小姐姐帮忙修改原HTML文件。
2.自己处理,利用WKWebView向网页内容中注入JS代码 (经测试有效果)

- (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;
}

相关文章

网友评论

      本文标题:iOS 使用wkwebview 加载HTML5,字体内容变小?

      本文链接:https://www.haomeiwen.com/subject/wqbbbltx.html