WKWebView 中的字体看起来比 UIWebView 中的小

2023-10-22移动开发问题
3

本文介绍了WKWebView 中的字体看起来比 UIWebView 中的小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我将 UIWebView 更改为 WKWebView,但是,使用相同的 html,WKWebView 中的字体看起来比 UIWebView 中的字体小.我不希望这种情况发生,那么有什么办法可以避免这种变化吗?

I changed UIWebView to WKWebView, however, with the same html, the font in WKWebView looks like smaller than in UIWebView. I don't want this happen, so is there any way to avoid this change?

推荐答案

最后我通过添加一个html字符串解决了这个问题:

Finally I solved this problem by adding an html string:

  • 对于 Objective-C:
NSString *headerString = @"<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>";
[self.webView loadHTMLString:[headerString stringByAppendingString:yourHTMLString] baseURL:nil];

  • 对于 Swift:
  • let headerString = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>"
    webView.loadHTMLString(headerString + yourHTMLString, baseURL: nil)
    


    另外,如果你想加载 url 而不是 html 你可以试试:


    What's more,if you want to load url rather than html you can try:

    private var isInjected: Bool = false
    webView.navigationDelegate = self
    // MARK: - WKNavigationDelegate
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        if isInjected == true {
            return
        }
        self.isInjected = true
        // get HTML text
        let js = "document.body.outerHTML"
        webView.evaluateJavaScript(js) { (html, error) in
            let headerString = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>"
            webView.loadHTMLString(headerString + (html as! String), baseURL: nil)
        }
        
    }
    

    这篇关于WKWebView 中的字体看起来比 UIWebView 中的小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7

游戏中心 facebook 喜欢
Game center facebook like(游戏中心 facebook 喜欢)...
2024-08-12 移动开发问题
3

Objective-C++ 导入 C++ 类失败,未找到 cassert
Objective-C++ importing C++ class fails, cassert not found(Objective-C++ 导入 C++ 类失败,未找到 cassert)...
2024-08-12 移动开发问题
10