NSAttributedString从HTML暗模式初始化,如何设置默认前景色?

时间:2019-11-05 13:41:54

标签: html ios swift nsattributedstring

在将现有应用程序转换为暗模式时,我偶然发现了UITextViewNSAttributedString的初始化过程,以为某些应用程序商店合法用户提供精美的印刷品。在亮模式下看起来不错,但是在暗模式下我得到黑色的黑色文本。我提供默认前景色的第一个想法是添加以下内容:

import UIKit

let fgColor: UIColor
if #available(iOS 13.0, *) {
    fgColor = .label
} else {
    fgColor = .black
}
let s = try NSAttributedString(data: NSLocalizedString("""
        <p style="text-align: center; font-size: smaller;">
        Your payment will be charged to your iTunes Account at confirmation of purchase. Your subscription automatically renews unless auto-renew is turned off at least 24-hours before the end of the current period. Your Account will be charged for renewal within 24-hours prior to the end of the current period at the same price as the original subscription.
        </p>
        <p style="text-align: center; font-weight: bold; font-size: smaller;">
        <a href="https://www.example.com/tos.html">Terms of Service</a>&nbsp;&vert;&nbsp;<a href="https://www.example.com/policy.html">Privacy Policy</a>
        </p>
""", comment: "legalese").data(using: String.Encoding.utf8, allowLossyConversion: false)!,
                               options: [.documentType: NSAttributedString.DocumentType.html.rawValue,
                                         .defaultAttributes: [NSAttributedString.Key.foregroundColor: fgColor]],
                               documentAttributes: nil)

但是无论如何,我都无法更改默认的前景色。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我尝试创建扩展。

这里,数据是我要转换为属性字符串的HTML数据,“ appLabelBlueColor”和“ appSystemSecondaryColor”是我要在暗模式下反映的系统颜色。

var htmlToAttributedString: NSAttributedString? {   
guard let data = data(using: .utf16,allowLossyConversion: true)
else { return NSAttributedString() }

 do {
        let attributedString = try NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType:  NSAttributedString.DocumentType.html], documentAttributes: nil)
        attrbutedString.addAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10, weight: .light), NSAttributedString.Key.foregroundColor: UIColor(named: "appLabelBlueColor")!, NSAttributedString.Key.backgroundColor: UIColor(named: "appSystemSecondaryColor")!], range: NSRange(
                                            location: 0,
                                            length: attribitedString.length))
        return attributedString

    } catch
    {
        return NSAttributedString()
    }
}

用法:

cell.detailLabel.attributedText = mfPerformanceTableObjectModel[indexPath.section].contentText.htmlToAttributedString

答案 1 :(得分:0)

如果您在 HTML 中使用内联样式,在将其转换为 NSAttributedString 之前,您可以添加 color: CanvasText; 以获得自动暗模式支持:

let source = String(format:"<span style='font-family: -apple-system;color: CanvasText;'>%@</span>", myContent)
if let data = modified.data(using: .utf16) {
   let attributedString = NSAttributedString(html: data, documentAttributes: nil)
   // Do something with attributedString
}