首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在UITextView swift中呈现HTML视图时将颜色设置为文本

在UITextView swift中呈现HTML视图时将颜色设置为文本
EN

Stack Overflow用户
提问于 2018-06-17 22:13:59
回答 1查看 1.2K关注 0票数 1

我正在使用swift在UITextView中呈现一个超文本标记语言视图。下面是代码。

代码语言:javascript
复制
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

现在我想将文本的颜色设置为白色。该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-17 22:27:37

只需使用textView的textColor属性:

代码语言:javascript
复制
// ...
textView.attributedText = attributedString
textView.textColor = .white

您也可以在属性字符串本身上设置foregroundColor -因此,您必须切换到NSMutableAttributedString

代码语言:javascript
复制
// ...
attributedString.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: attributedString.length))
textView.attributedText = attributedString
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50897473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档