[英]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.