[英]Possible to use named color from asset catalog in webview?
是否有任何捷径可以让我们在 WebView 中直接使用命名/动态 iOS 颜色?
我的意思是,iOS 知道所有名为 colors - 为什么不将它们注入 webview?
这是我尝试过的,但没有奏效:
<div style="color:systemBackground">Test</div>
我知道 HTML 和应用程序资产是两个不同的域,但也许有人知道一个优雅的捷径。
我想这可能是一个两步的过程。 第二步将用动态/命名 colors 替换 web colors:
let html = "Hello <u><b style='color:red'>World!</b></u>"
let data = html.data(using: .utf8)!
let string = try! NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
// we now replace all colors with appropriate dynamic ones
string.enumerateAttribute(.foregroundColor, in: NSRange(0..<string.length)) { value, range, stop in
if let color = value as? UIColor {
if color.cgColor.components == UIColor.red.cgColor.components {
string.addAttribute(.foregroundColor, value: UIColor.systemRed, range: range)
} else {
string.addAttribute(.foregroundColor, value: UIColor.label, range: range)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.