admin管理员组文章数量:1401796
My app has a WebView load by Url, not html string. I have tried several options to change the font, but it didn't work. I use this method below. Please offer some help and sorry for my poor English!
NSString *fontFamilyStr = @"document.getElementsByTagName('body')[0].style.fontFamily='PingFangSC-Light';";
[webView stringByEvaluatingJavaScriptFromString:fontFamilyStr];
My app has a WebView load by Url, not html string. I have tried several options to change the font, but it didn't work. I use this method below. Please offer some help and sorry for my poor English!
NSString *fontFamilyStr = @"document.getElementsByTagName('body')[0].style.fontFamily='PingFangSC-Light';";
[webView stringByEvaluatingJavaScriptFromString:fontFamilyStr];
Share
Improve this question
edited Jul 12, 2018 at 6:41
rmaddy
319k44 gold badges546 silver badges588 bronze badges
asked Jul 12, 2018 at 6:40
fritzfritz
331 silver badge9 bronze badges
3 Answers
Reset to default 3I am not really sure where are you trying to call stringByEvaluatingJavaScriptFromString
in your code, you should call it once the webView finishes loading, because you are trying to modify the UI HTML page (by changing font) if you try to run your JavaScript mand even before webpage finishes loading, though your javaScript code runs, it wont be able to modify the properties of its ponents because they might not yet have finished rendering.
Hence,
Step 1:
Make your ViewController the navigationDelegate
of your webView
webView.navigationDelegate = self
Step 2:
Add extension to your ViewController and confirm to WKNavigationDelegate
extension ViewController : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let changeFontFamilyScript = "document.getElementsByTagName(\'body\')[0].style.fontFamily = \"Impact,Charcoal,sans-serif\";"
webView.evaluateJavaScript(changeFontFamilyScript) { (response, error) in
debugPrint("Am here")
}
}
}
Step 3:
Observe the usage of '\' to escape special characters like singleQuote (') and doubleQuotes (") in "document.getElementsByTagName(\'body\')[0].style.fontFamily = \"Impact,Charcoal,sans-serif\";"
which you are missing in your code above
Step 4:
Make sure your provide proper FontaFamilies and options to fall back if your webPage cant find the specified FontFamily (Look at 3 font names I have specified Impact,Charcoal,sans-serif)
O/P
Without NavigationDelegate (without running javaScript)
After Applying Font
Hope this helps
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='PingFangSC-Light' size='3'>%@", body];
[webView loadHTMLString:htmlString baseURL:nil];
lets Try it.It works for me. Hope it will helps you.
let styles = "<html><head>"
+ "<style type=\"text/css\">body{font-size:20px;font-family:Helvetica;}"
+ "</style></head>"
+ "<body>" + "</body></html>"
self. webView.loadHTMLString(styles, baseURL: nil)
本文标签: javascriptChange font from Webview in iosStack Overflow
版权声明:本文标题:javascript - Change font from Webview in ios - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744273977a2598319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论