user3741418
user3741418

Reputation: 439

NSAttributedString from HTML

I am trying to convert HTML formatted text into an NSAttributedString so I can properly format the text in a UITextView I found DTCoreText which can do that but when I run the code I get an error.

The code I am using:

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

_storyContent is an NSString.

Error:

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

How can I take the content and format it so it is readable?

Sample text:

<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>

Upvotes: 3

Views: 2301

Answers (2)

lucamozza
lucamozza

Reputation: 31

This is an old question, but I just encountered the same issue. The solution is to add the DTUseiOS6Attributes option.

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

As @odrobnik explains here:

it essentially means to use the NS-style attributes when building the attributed string, instead of the old CoreText-style attributes which are incompatible with UIKit.

Upvotes: 1

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

Try use the following code which I found from here

// 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];

Upvotes: 4

Related Questions