Reputation:
I have a wkwebview that i want to fill with content (imprint, licenses, disclaimers, that sort of stuff) that gets fetched from an API.
My Problem is that the font size changes after rotating the iPhone. But I want it to stay the same. This is how i create the HTML:
func embed(content: String) -> String {
return "<!DOCTYPE html><html><head><meta charset=\"utf-8\" name=\"viewport\", width=\"device-width\", initial-scale=1.0, maximum-scale=1.0></head><body>\(content)</body></html>"
}
And via CSS I want to prevent the font size from changing (CSS as String):
private static let fontSizeSetting = "html{-webkit-text-size-adjust: 100%;}"
and this is the way i run the CSS:
func insert(cssString: String, into webView: WKWebView) {
let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
webView.evaluateJavaScript(jsString, completionHandler: nil)
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.insert(cssString: css, into: self.webView)
}
Any Ideas how to prevent font size change?
this post didn't help me: UIWebView resizes text after rotating: looking for explanation for magical bug or my stupidity
Upvotes: 5
Views: 987
Reputation: 1091
I found this CSS to fix the WKWebView font change on rotation issue.
body {-webkit-text-size-adjust: none;}
Upvotes: 6