我将 UIWebView
更改为 WKWebView
,但是,使用相同的 html,WKWebView
中的字体看起来比 UIWebView 中的字体小代码>.我不希望这种情况发生,那么有什么办法可以避免这种变化吗?
I changed UIWebView
to WKWebView
, however, with the same html, the font in WKWebView
looks like smaller than in UIWebView
. I don't want this happen, so is there any way to avoid this change?
最后我通过添加一个html字符串解决了这个问题:
Finally I solved this problem by adding an html string:
NSString *headerString = @"<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>";
[self.webView loadHTMLString:[headerString stringByAppendingString:yourHTMLString] baseURL:nil];
let headerString = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>"
webView.loadHTMLString(headerString + yourHTMLString, baseURL: nil)
另外,如果你想加载 url 而不是 html 你可以试试:
What's more,if you want to load url rather than html you can try:
private var isInjected: Bool = false
webView.navigationDelegate = self
// MARK: - WKNavigationDelegate
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if isInjected == true {
return
}
self.isInjected = true
// get HTML text
let js = "document.body.outerHTML"
webView.evaluateJavaScript(js) { (html, error) in
let headerString = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></head>"
webView.loadHTMLString(headerString + (html as! String), baseURL: nil)
}
}
这篇关于WKWebView 中的字体看起来比 UIWebView 中的小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!