Font sizes in UIWebView does NOT match iOS font size

前端 未结 3 552
北恋
北恋 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:05

    If you want a 1 to 1 relationship between the font size that the UILabel uses and the font size that the UIWebView uses (via CSS) you have to use px instead of pt when defining the font size in your CSS.

    Checkout this example HTML / CSS:

    
        
            
        
        
            

    About (17pt)

    About (17px)

    When you add a UIWebView and a UILabel to your UIViewController and load the above HTML to the UIWebView and set the same font to the UILabel:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let webView = UIWebView(frame: CGRect(x: 25, y: 50, width:325 , height: 90))
        view.addSubview(webView)
    
        let filePath = NSBundle.mainBundle().URLForResource("test", withExtension: "html")
        webView.loadRequest(NSURLRequest(URL: filePath!))
    
        let label = UILabel(frame: CGRect(x: 25, y: 150, width: 325, height: 40))
        label.font = UIFont(name: "SourceSansPro-Regular", size: 17)
        label.text = "About (UILabel set to 17)"
        view.addSubview(label)
    }
    

    You get the following output:

提交回复
热议问题