Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

iphone - Convert formatted HTML text string to NSString parts

I want to decode HTML string and store it in NSString.

following is the html string for one of the response from google direction api.

teststring is Turn <b>right</b> onto <b>Kennington Park Rd/A3</b><div style="font-size:0.9em">Continue to follow A3</div><div style="font-size:0.9em">Entering toll zone in 1.7&nbsp;km at Newington Causeway/A3</div><div style="font-size:0.9em">Go through 2 roundabouts</div>

I want to store this html string in Array of 4 different NSStrings with following 4 NSStrings (removing all information for size colour)

Turn right onto Kennington Park Rd/A3
Continue to follow A3
Entering toll zone in 1.7?km at Newington Causeway/A3
Go through 2 roundabouts

I have used following method to convert html string to plain text.(html_response is the response from server and stringByConvertingHTMLToPlainText is the method defined in custom class file)

testString = (NSString*) [html_response stringByConvertingHTMLToPlainText];
NSLog(@"%@",testString);

but it converts whole string instead of breaking it in parts. output at the console is

Turn right onto Kennington Park Rd/A3Continue to follow A3 Entering toll zone in 1.7?km at Newington Causeway/A3 Go through 2 roundabouts

So do I need to write custom method to do this? Any help will be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's a very nice NSString category that you can use, it's a part of the project MWFeedParser. More specifically, you look for the file NSString+HTML.

The NSString+HTML category adds the following methods to the NSString Class,

- (NSString *)stringByStrippingTags;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;

You could then try something like:

NSString *summary = [[[htmlString stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingHTMLEntities];

Hope it helps!, good luck :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...