Paul Morris
Paul Morris

Reputation: 1773

Adding New Lines / Breaks / Paragraphs into a Long NSString

I have an NSString that is derived from an Atom XML Feed. The data that I get back is full of HTML syntax, various tags, links and so forth. I take that entire string and run it through the stringByConvertingHTMLToPlainText method that is part of the MWFeedParser library.

This works perfectly and gives me a very long NSString - basically the full news story of the feed i'm parsing.

I want to take that long single NSString and break it up into paragraphs to make it more readable in my app rather than one long scrollable list of un-formatted text. Is there anyway I can apply paragraphs or new lines and break the string up like that?

Upvotes: 0

Views: 660

Answers (1)

MJN
MJN

Reputation: 10818

You can split up your long string into an array of strings using componentsSeparatedByString:. Then for each element (paragraph) in the array, run the string through stringByConvertingHTMLToPlainText to strip out remaining html tags.

You might want to do some more work on the contents or paragraphs after stripping the html tags.

NSString *htmlString = @"<p>first paragraph</p><p>second paragraph</p><p>thrid paragraph</p>";
NSArray *paragraphs = [htmlString componentsSeparatedByString: @"<p>"]; // still includes </p>
for (NSString *singleParagraph in paragraphs) {
    // strip out remaining html tags
    singleParagraph = [singleParagraph stringByConvertingHTMLToPlainText];
}

// strip out empty or irrelevant indexes and paragraphs you don't need

Upvotes: 1

Related Questions