gpt4 book ai didi

javascript - 如何使用 JavaScript 更改 UIWebView 字体?

转载 作者:行者123 更新时间:2023-11-30 14:02:01 25 4
gpt4 key购买 nike

我想知道如何使用 JavaScript 更改字体。

虽然这样:

let fontName = "AmericanTypewriter-CondensedBold"

if let infoString = config.textAboutApplication {
infoView.loadHTMLString(NSString(format: "<div style='text-align:left;font-family:%@'>%@<div>", fontName, infoString) as String, baseURL: nil)
}

使用格式化文本产生正确的结果,JavaScript 版本不会:

    func webViewDidFinishLoad(webView: UIWebView)
{
let fontName = "AmericanTypewriter-CondensedBold"

let jsString = NSString(format: "document.getElementById('p').style.fontFamily = '%@'", fontName)
webView.stringByEvaluatingJavaScriptFromString(jsString as String)
}

HTML 文本确实有 p 标签和文本,所以我可能在 JavaScript 部分做错了。

最佳答案

好吧,我正在尝试,JavaScript 不是我最擅长的,但这可行

let fontName = "AmericanTypewriter-CondensedBold"

let jsString = NSString(format: "document.getElementsByTagName('p')[0].style.fontFamily = '%@'", fontName)
let js = webView.stringByEvaluatingJavaScriptFromString(jsString as String)

编辑:我对这个困惑的解决方案进行了自定义实现,以使其更加快速酷。 :)https://github.com/Vanyaslav/Swiftyyjsformatter

关于javascript - 如何使用 JavaScript 更改 UIWebView 字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32884031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com