简体   繁体   中英

How do i change font size in WKWebView swift 5?

I use UIWebview and this code, it works fine before iOS 13. Since UIWebview is deprecated it did not work in iOS 13. how do I do it using WKWebView ?

func changedFont(size: Int){
    let jsString = "document.getElementsByTagName('body')[0].style.fontSize='\(size)px'"
    webView.stringByEvaluatingJavaScript(from: jsString)
}

尝试这个

self.webView.loadHTMLString("<html><body><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0,Proxima Nova-Regular\">\(content)</body></html>", baseURL: nil)

Try This. The below code will work when you are having multiple font sizes and you have set same font size for all.

func replaceFontSizes(withSize fontSize: Float, in replaceString: String?) -> String? {
var replaceString = replaceString
var r1 = (replaceString as NSString?)?.range(of: "font-size:")
while r1?.location != NSNotFound {

    let strSub = (replaceString as NSString?)?.substring(from: r1?.location ?? 0)
    var arrStr: [AnyHashable]?

    if strSub?.contains("pt") ?? false {
        arrStr = strSub?.components(separatedBy: "pt")
    } else if strSub?.contains("px") ?? false {
        arrStr = strSub?.components(separatedBy: "px")
    }

    let strReplace = arrStr?[0] as? String

    replaceString = replaceString?.replacingOccurrences(of: strReplace ?? "", with: String(format: "font@@-@@size:%.2f", fontSize))

    r1 = (replaceString as NSString?)?.range(of: "font-size:")
}

replaceString = replaceString?.replacingOccurrences(of: "font@@-@@size:", with: "font-size:")
return replaceString

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM