[英]how to set text color like background color for light and dark mode on html email
[英]NSAttributedString initialised from HTML dark mode, how to set default foreground color?
在将现有应用程序转换为暗模式时,我偶然发现了一个带有NSAttributedString
的UITextView
的初始化,以便为某些应用程序商店的法律术语提供细则。 这在浅色模式下看起来不错,但在深色模式下,我会在黑色上看到黑色文本。 我提供默认前景色的第一个想法是添加以下内容:
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> | <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)
但无论我尝试什么,我都无法更改默认的前景色。 有任何想法吗?
我试图创建一个扩展。
在这里,数据是我要转换为属性字符串的 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()
}
}
用法:
cell.detailLabel.attributedText = mfPerformanceTableObjectModel[indexPath.section].contentText.htmlToAttributedString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.