NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];只能在iOS7以后运行

本文展示了如何在iOS7及以上版本中,将包含HTML样式信息的字符串解析为NSAttributedString对象,并通过UILabel展示特定样式的文本。具体操作包括使用dataUsingEncoding和NSHTMLTextDocumentType选项进行数据转换,以及设置文本属性来实现红色13号字体的效果。
3301

被折叠的 条评论
为什么被折叠?



