在 UITextView swift 中呈现 HTML 视图时为文本设置颜色
Set color to text while rendering HTML view in UITextView swift
我正在使用 swift 在 UITextView 中呈现 HTML 视图。这是代码。
guard let attributedString = try? NSAttributedString(data: htmlContent.data(using: String.Encoding.utf8)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) else {
return
}
textView.attributedText = attributedString
现在我想将文本的颜色设置为白色。怎么做?
只需使用textView的textColor
属性:
// ...
textView.attributedText = attributedString
textView.textColor = .white
您还可以在属性字符串本身上设置 foregroundColor
- 因此您必须切换到 NSMutableAttributedString
:
// ...
attributedString.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: attributedString.length))
textView.attributedText = attributedString
我正在使用 swift 在 UITextView 中呈现 HTML 视图。这是代码。
guard let attributedString = try? NSAttributedString(data: htmlContent.data(using: String.Encoding.utf8)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) else {
return
}
textView.attributedText = attributedString
现在我想将文本的颜色设置为白色。怎么做?
只需使用textView的textColor
属性:
// ...
textView.attributedText = attributedString
textView.textColor = .white
您还可以在属性字符串本身上设置 foregroundColor
- 因此您必须切换到 NSMutableAttributedString
:
// ...
attributedString.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: attributedString.length))
textView.attributedText = attributedString