从HTML创建NSAttributedString

3

我正在尝试将HTML格式的文本转换成一个NSAttributedString,以便我可以在UITextView中正确地格式化文本。我发现了DTCoreText,可以做到这一点,但是当我运行代码时,会出现错误。

我使用的代码:

NSData *data = [_storyContent dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *styledContent = [[NSAttributedString alloc]initWithHTMLData:data documentAttributes:nil];
_bodyTextView.attributedText = styledContent;

_storyContent是一个NSString。

错误:

[__NSCFType textBlocks]: unrecognized selector sent to instance 0xb83b310

我可以如何获取内容并将其格式化为可读性较高的方式?

示例文本:

<p>May 28, 2014 — During his brief life, David Arapene Cuch, 1978-2007, accomplished many things. He was a scholar, completing a bachelor’s degree in economics at Westminster College and a Master of Public Administration at the University of Utah. At the time of his unexpected death, Cuch, who was believed to be the first Ute Indian to attend law school, was in his third year at the University of Utah S.J. Quinney College of Law. In addition to his academic accomplishments, Cuch also worked as a coordinator for a social justice nonprofit group, an assistant teacher in Salt Lake City and a summer camp youth counselor. During law school, he worked for the Legal Defender Association and Salt Lake Legal Issues.</p>
<p>On Friday, May 30, Cuch’s accomplishments will be celebrated by his family, representatives of the Ute Indian Tribe and the University of Utah at an 11:30 a.m ceremony in the Varsity Room on the sixth floor of the Rice-Eccles Stadium. During the memorial, Cuch’s brother, Cameron, will announce the David Arapene Cuch Endowed Scholarship Fund, which was created to provide financial support for members of the Ute Indian Tribe who follow David Cuch’s path by attending law school at the U. The scholarship is funded by David Cuch’s family and friends.</p>
<p>“David Arapene Cuch was a remarkable individual. His death at such a young age is a tragedy, but his legacy of commitment to community, to the rights and sovereignty of American Indians and to improving the world for all of humanity continues to inspire us. The generous scholarship, in his honor, will provide opportunity for students to realize their dreams,” said University of Utah President David Pershing.</p>
<p>In addition to Cameron Cuch and President David Pershing, Bob Adler, interim dean of the College of Law, Gordon Howell, chairman of the Ute Indian Tribe Business Committee, and Cuch’s parents, Forrest and Carla Cuch, will also speak. Utah tribal drummers and singers are scheduled to perform.</p>
<p>Contributions in David Cuch’s honor may be made to the David Cuch Scholarship care of the University of Utah S.J. Quinney College of Law Development Office, 332 S. 1400 East Room 101, Salt Lake City, Utah, 84112.</p>

你会将更新后的HTML内容从UITextView发送回服务器进行存储吗?如果是这样,我建议使用可编辑的<div></div> UIWebView而不是UITextView。 - Zhang
不,这只是为了以可读的方式呈现获取的文本。 - user3741418
这个Swift版本也许可行: let htmlString = "(message.message) <a href="(link.uri)">(link.name)</a>" let htmlData = htmlString.dataUsingEncoding(NSUTF8StringEncoding) aString = NSMutableAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil, error: err)!据我所见,你可能只是缺少指定文档类型的选项。 - Bjørn Ruthberg
2个回答

4
尝试使用我从这里找到的以下代码。
// This is a string that you might find in your model
NSString *html = @"This is <b>bold</b>";

// Apply some inline CSS
NSString *styledHtml = [self styledHTMLwithHTML:html];

NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[styledHtml dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL error:NULL];

@EvaMadrazo 使用 initWithObjects:forKeys: 方法手动创建字典。 - TheAmateurProgrammer
不是那么容易,@TheAmateurProgrammer。在IOS6中不存在NSHTMLTextDocumentType,因此如果您需要在UITextView中显示HTML文本,则应自己解析HTML :(. 我的意思是,如果您不解析文本,则会在界面中看到HTML标记。 - Eva Madrazo
@EvaMadrazo 快速的谷歌搜索显示了一个第三方解决方案,称为 DTCoreText,如果您想支持iOS 6,它似乎是一个可行的选择。 - TheAmateurProgrammer

1

这是一个老问题,但我刚遇到了同样的问题。 解决方法是添加DTUseiOS6Attributes选项。

NSAttributedString *attributedString = [[NSAttributedString alloc]initWithHTMLData:data options:@{DTUseiOS6Attributes : @YES} documentAttributes:nil];

正如@odrobnik在这里所解释的:

它基本上意味着在构建属性字符串时使用NS样式属性,而不是与UIKit不兼容的旧的CoreText样式属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接