12

Been searching the net for an example of how to convert HTML string markup into Plain text.

I get my information from a feed which contains HTML, I then display this information in a Text View. does the UITextView have a property to convert HTML or do I have to do it in code. I tried:

NSString *str = [NSString stringWithCString:self.fullText encoding:NSUTF8StringEndcoding]; 

but doesn't seem to work. Anyone got any ideas?

4 Answers 4

33

You can do it by parsing the html by using NSScanner class

- (NSString *)flattenHTML:(NSString *)html { NSScanner *theScanner; NSString *text = nil; theScanner = [NSScanner scannerWithString:html]; while ([theScanner isAtEnd] == NO) { [theScanner scanUpToString:@"<" intoString:NULL] ; [theScanner scanUpToString:@">" intoString:&text] ; html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""]; } // html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; return html; } 

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't deal with single quotes but for everything else works fine.
If you are having single quotes and you don't want to show them just replace there occurrence by blank string
Hi @Madhup. please have look at the question -stackoverflow.com/questions/8148291/… and advice.
8

If you are using UIWebView then it will be easier to parse HTML to text:

fullArticle = [webView stringByEvaluatingJavaScriptFromString:@"document.body.getElementsByTagName('article')[0].innerText;"]; // extract the contents by tag fullArticle = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"]; // extract text inside body part of HTML 

Comments

-1

you can't do it directly i guess.. however you can use NSXML Parser and parse the HTML and retrieve exactly what you want...

2 Comments

would this method keep the formatting? What I want is to display the formatted HTML in plain text, so keep links, <h1> <p> etc.. how do other app do this?
NSXML parser will not parse normal HTML. It fails on HTML only characters.
-1

If you need to present the text in read-only fashion, why not use UIWebView?

1 Comment

UIWebView display's a webpage inside a app? need a control or method of keeping the html format but not displaying it. my output contains the markup were i want to it keep to style but not show the html.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.