我想在 UICollectionView 中显示来自服务器的数据。数据以如下所示的 html 字符串形式出现
<span> recognized <a
href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>
Run Code Online (Sandbox Code Playgroud)
我希望它像这样显示
我怎样才能做到这一点?有人请帮助我。
更新此答案
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)
| 归档时间: |
|
| 查看次数: |
5801 次 |
| 最近记录: |