How to convert NSString HTML markup to plain text NSString?

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:

  • Unable to fix code signing issue: Xcode7.3
  • Crop Image from Camera in Swift without move to another ViewController
  • AVQueuePlayer playback without gap and freeze
  • Swift: what is the right way to split up a resulting in a ] with a given subarray size?
  • Will use of the PayPal iOS library get my app rejected?
  • Permutations/Anagrams in Objective-C — I am missing something
  • NSString *str = [NSString stringWithCString:self.fullText encoding:NSUTF8StringEndcoding];
    

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

    4 Solutions Collect From Internet About “How to convert NSString HTML markup to plain text NSString?”

    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.

    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
    

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

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