简体   繁体   English

可以使用 webview 中资产目录中的命名颜色吗?

[英]Possible to use named color from asset catalog in webview?

Is there any shortcut which would enable us to directly use a named / dynamic iOS color within a WebView?是否有任何捷径可以让我们在 WebView 中直接使用命名/动态 iOS 颜色?

I mean, the iOS knows all named colors - why not inject those into the webview?我的意思是,iOS 知道所有名为 colors - 为什么不将它们注入 webview?

This is what I tried and which failed to work:这是我尝试过的,但没有奏效:

<div style="color:systemBackground">Test</div>

I know that HTML and app assets are two different domains, but maybe someone knows an elegant shortcut.我知道 HTML 和应用程序资产是两个不同的域,但也许有人知道一个优雅的捷径。

I guess it could be a 2-step process.我想这可能是一个两步的过程。 The 2nd step would replace web colors with dynamic / named colors:第二步将用动态/命名 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM