Font sizes in UIWebView does NOT match iOS font size

前端 未结 3 527
北恋
北恋 2021-01-11 12:26

Here\'s a UILabel which says \"About\". Set at exactly 17.7 in iOS.

Below it a UIWebView which also says \"About\". Also set at exactly 17.7 using css.

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 13:02

    I think your confusion was because CSS pt is a typographic point (1/72 in), but Apple's iOS documentation uses "point" (e.g. UIFont.pointSize) as "virtual pixel", which corresponds to CSS px. Judging from @joern's answer, it looks like UIWebView uses 1 css px = 1 ios virtual px. (I have not tested this.) However, in my tests with WKWebView, it looks like CSS px is equal to one device pixel. So for WKWebView, you want:

    let fontSize = UIScreen.main.scale * label.font.pointSize
    let cssStr = String(format:"font-size: %.1fpx;", fontSize)
    

提交回复
热议问题