views:

575

answers:

3

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.

Tried:

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

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

Thanks for your time

A: 

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

mihirpmehta
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?
Frames84
+1  A: 

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,

Thanks,

Madhup

Madhup
Doesn't deal with single quotes but for everything else works fine.
Frames84
If you are having single quotes and you don't want to show them just replace there occurrence by blank string
Madhup
A: 

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

dusker
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