如何在 iOS-swift 3.0 中的 UILabel 中显示 html 文本

Man*_*... 2 ios swift3

我想在 UICollectionView 中显示来自服务器的数据。数据以如下所示的 html 字符串形式出现

  <span> recognized <a 

href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>
Run Code Online (Sandbox Code Playgroud)

我希望它像这样显示

在此处输入图片说明

我怎样才能做到这一点?有人请帮助我。

Pau*_*l B 5

斯威夫特 4

更新此答案

    extension NSAttributedString {
        internal convenience init?(html: String) {
            guard let data = html.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
                // not sure which is more reliable: String.Encoding.utf16 or String.Encoding.unicode
                return nil
            }
            guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
                return nil
            }
            self.init(attributedString: attributedString)
        }
    }

    label.attributedText = NSAttributedString(html: "<span> recognized <a href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>")
Run Code Online (Sandbox Code Playgroud)