为什么 WebKit 字体与 iOS UILabel 的字体不同?

huangapple go评论61阅读模式
英文:

Why WebKit font vs iOS UILabel has different Font?

问题

以下是您要翻译的内容:

我正在使用webview,并且要更改`WKWebKit`的字体大小(50号),我正在使用以下代码:

`let fontSetting = "<span style=\"font-size: \(fontSize)\"></span>"`

我从Storyboard中获取了字体大小为50的`UILabel`。

为什么两者显示不同?

[![在此输入图片描述][1]][1]

我的完整代码如下:

import UIKit
import WebKit

class ViewController: UIViewController {
    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        webView = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200))
        webView.navigationDelegate = self
        view.addSubview(webView)
    }

    override func viewDidAppear(_ animated: Bool) {
        let htmlStr = ""
        "<p><a href=\"https://www.google.com\">WebView-Button-size-50</a></p>"
        ""
        let fontSize = 50
        let fontSetting = "<span style=\"font-size: \(fontSize)\"></span>"
        webView.loadHTMLString(fontSetting + htmlStr, baseURL: nil)
        webView.allowsBackForwardNavigationGestures = true
    }
}

<details>
<summary>英文:</summary>

I am using webview &amp; for changing font(size 50) of `WKWebKit` i am using

 `let fontSetting = &quot;&lt;span style=\&quot;font-size: \(fontSize)\&quot;&lt;/span&gt;&quot;`

I have taken `UILabel` from storyboard of font-size 50 

Why both size shows different ? 

[![enter image description here][1]][1]
My complete code is below --- 
 

    import UIKit
    import WebKit
    
    class ViewController: UIViewController {
        var webView: WKWebView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            webView = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200))
            webView.navigationDelegate = self
            view.addSubview(webView)
        }
        
    
        override func viewDidAppear(_ animated: Bool) {
            let htmlStr = &quot;&quot;&quot;
            &lt;p&gt;&lt;a href=&quot;https://www.google.com&quot;&gt;WebView-Button-size-50&lt;/a&gt;&lt;/p&gt;
            &quot;&quot;&quot;
            let fontSize = 50
            let fontSetting = &quot;&lt;span style=\&quot;font-size: \(fontSize)\&quot;&lt;/span&gt;&quot;
            webView.loadHTMLString(fontSetting + htmlStr, baseURL: nil)
            webView.allowsBackForwardNavigationGestures = true
        }
    
    }


  [1]: https://i.stack.imgur.com/1vAQ4.png

</details>


# 答案1
**得分**: 1

这是因为UILabel采用了默认字体(表示已分配字体),但对于web,您没有指定字体名称。

请检查下面的代码,它在我的端口上正常工作。

```swift
func adjustHTMLData(htmlText: String) {
    let innerFontName: String = "myFontName".localized()
    
    let innerFontSize1: CGFloat = 32 * iPhoneFactorX
    let innerDirection: String = "myDir".localized()
    
    var header002: String = htmlText
    header002 = header002.replacingOccurrences(of: "font-family", with: "f ont-family")
    header002 = header002.replacingOccurrences(of: "<font", with: "<ffont")
    
    let htmlString: String = "<html><head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 10px;padding: 0px;font-family: '\(innerFontName)'; font-size: \(innerFontSize1)px;color:\(AppColors.color_000000);}</style></head><body dir='\(innerDirection)'>\(header002)<br /><br /><style>@font-face {font-family: '\(innerFontName)';font-weight: normal;src: url('\("fontTTF".localized())');}</style><style type='text/css'> iframe { width: 100% !important; } img {width: 100% !important; height: auto;} a {text-decoration: none; color : \(AppColors.color_000000)} table {width: 100% !important;} }</style></body></html>";
    
    print("htmlString==\(htmlString)")
    self.myWebview.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
}

在本地化中,我有以下内容:

"fontTTF"="Poppins-Regular.ttf";
"myFontName"="Poppins-Regular";
"myDir"="LTR";

AppsColor有以下内容:

static let color_000000: String = "#000000"

希望这能帮助到您。

英文:

This is because UILabel took default font (means font is assigned) however for web, you didn't specify font name.

Check below code which is working fine at my end.

func adjustHTMLData(htmlText : String) {
    let innerFontName: String = &quot;myFontName&quot;.localized()
    
    let innerFontSize1: CGFloat = 32*iPhoneFactorX
    let innerDirection: String = &quot;myDir&quot;.localized()
    
    var  header002: String = htmlText
    header002 = header002.replacingOccurrences(of: &quot;font-family&quot;, with: &quot;f ont-family&quot;)
    header002 = header002.replacingOccurrences(of: &quot;&lt;font&quot;, with: &quot;&lt;ffont&quot;)
    
    let htmlString: String = &quot;&lt;html&gt;&lt;head&gt;&lt;style type=&#39;text/css&#39;&gt;body {background-color: transparent;border: 0px;margin: 10px;padding: 0px;font-family: &#39;\(innerFontName)&#39;; font-size: \(innerFontSize1)px;color:\(AppColors.color_000000);}&lt;/style&gt;&lt;/head&gt;&lt;body dir=&#39;\(innerDirection)&#39;&gt;\(header002)&lt;br /&gt;&lt;br /&gt;&lt;style&gt;@font-face {font-family: &#39;\(innerFontName)&#39;;font-weight: normal;src: url(\(&quot;fontTTF&quot;.localized()));}&lt;/style&gt;&lt;style type=&#39;text/css&#39;&gt; iframe { width: 100%% !important; } img {width: 100% !important; height: auto;} a {text-decoration: none; color : \(AppColors.color_000000)} table {width: 100%% !important;} }&lt;/style&gt;&lt;/body&gt;&lt;/html&gt;&quot;;
    
    print(&quot;htmlString==\(htmlString)&quot;)
    self.myWebview.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
    
}

In localization I have

&quot;fontTTF&quot;=&quot;Poppins-Regular.ttf&quot;;
&quot;myFontName&quot;=&quot;Poppins-Regular&quot;;
&quot;myDir&quot;=&quot;LTR&quot;;

AppsColor have below

static let color_000000 : String = &quot;#000000&quot;

huangapple
  • 本文由 发表于 2023年6月6日 13:28:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定