ios 知识点

iOS开发解析富文本

2017-09-08  本文已影响350人  东城夕阳

在开发中经常有后台返回的富文本字符串需要iOS开发者解析,很多同学第一想到的是webview,但是webview的性能不好而且并不利于适配。有更简单的方法就是直接将富文本字符解析成iOS中的富文本,然后将富文本显示在UILabel上,这样内存消耗比较低,并且非常有利于屏幕的适配

OC:

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

label.attributedText = attrStr;

Swift:

let attribstr = try! NSAttributedString.init(data:(htmlString?.data(using: String.Encoding.unicode))! , options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)

上一篇 下一篇

猜你喜欢

热点阅读