How to change UIWebView or WKWebView default font

前端 未结 9 1270
陌清茗
陌清茗 2020-12-08 01:14

What font does UIWebView and WKWebView use by default? I would like to be able to change that. But I don\'t want to do it in the html string, inste

相关标签:
9条回答
  • 2020-12-08 01:49

    Quick solution for SWIFT 4.2

        let fontName =  "PFHandbookPro-Regular"
        let fontSize = 20
        let fontSetting = "<span style=\"font-family: \(fontName);font-size: \(fontSize)\"</span>"
        webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)
    

    span is an inline element, meaning that it can be on a line with other elements

    0 讨论(0)
  • 2020-12-08 01:50

    Just prefix a <font face> tag to your string before loading into the webView.

    NSString *body = [plist objectForKey:@"foo"];
    NSString *htmlString = 
        [NSString stringWithFormat:@"<font face='GothamRounded-Bold' size='3'>%@", body];
    [webView loadHTMLString:htmlString baseURL:nil];
    
    0 讨论(0)
  • 2020-12-08 01:52

    There is the swift 3 solution :

    func webViewDidFinishLoad(_ webView: UIWebView) {
        webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.fontFamily =\"-apple-system\"")
    }
    

    I have just put the default iOS font for this example

    0 讨论(0)
提交回复
热议问题