简体   繁体   English

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

[英]NSAttributedString initialised from HTML dark mode, how to set default foreground color?

While converting an existing app to dark mode I stumbled across an initialisation of an UITextView with an NSAttributedString to supply the fine print for some app store legalese.在将现有应用程序转换为暗模式时,我偶然发现了一个带有NSAttributedStringUITextView的初始化,以便为某些应用程序商店的法律术语提供细则。 This looks fine in light mode, but in dark mode I get black text on black.这在浅色模式下看起来不错,但在深色模式下,我会在黑色上看到黑色文本。 My first idea to supply a default foreground color was to add this:我提供默认前景色的第一个想法是添加以下内容:

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)

But whatever I tried, I can not change the default foreground color.但无论我尝试什么,我都无法更改默认的前景色。 Any ideas?有任何想法吗?

I have tried to create an extension.我试图创建一个扩展。

Here, data is the HTML data that I want to convert to Attributed String, "appLabelBlueColor" and "appSystemSecondaryColor" are the system colors that I want to reflect in Dark Mode.在这里,数据是我要转换为属性字符串的 HTML 数据,“appLabelBlueColor”和“appSystemSecondaryColor”是我想在暗模式下反映的系统 colors。

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()
    }
}

Usage:用法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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