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?

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
Frames84
  • 177
  • 1
  • 3
  • 11

4 Answers4

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.

Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
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
Luke
  • 11,426
  • 43
  • 60
  • 69
Veera Raj
  • 1,562
  • 1
  • 19
  • 40
-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...

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • would this method keep the formatting? What I want is to display the formatted HTML in plain text, so keep links,

    etc.. how do other app do this?

    – Frames84 Apr 09 '10 at 10:46
  • NSXML parser will not parse normal HTML. It fails on HTML only characters. – Neil Dec 29 '12 at 23:36
-1

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

dusker
  • 580
  • 3
  • 11
  • 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. – Frames84 Apr 09 '10 at 10:50