美文网首页IOS开发中的小知识点整理
如何让Label等控件支持HTML格式的代码? 使用NSAttr

如何让Label等控件支持HTML格式的代码? 使用NSAttr

作者: 清风沐沐 | 来源:发表于2016-09-29 16:15 被阅读536次

> 如何让Label等控件支持HTML格式的代码? 使用NSAttributedString:

NSString *htmlString = @"<div>Tate<span style='color:#1C86EE;'>《iOS Develop Tips》</span>get <span style='color:#1C86EE;'>Tate_zwt</span> 打赏 <span style='color:#FF3E96;'>100</span> 金币</div>";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];_contentLabel.attributedText = attributedString;

>如何让Label等控件同时支持HTML代码和行间距?

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[_open_bonus_article dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];[paragraphStyle setLineSpacing:12]; //调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
_detailLabel.attributedText = attributedString;[_detailLabel sizeToFit];

相关文章

网友评论

    本文标题:如何让Label等控件支持HTML格式的代码? 使用NSAttr

    本文链接:https://www.haomeiwen.com/subject/kctmettx.html