How to convert NSString HTML markup to plain text NSString?

后端 未结 4 614
名媛妹妹
名媛妹妹 2020-12-09 05:37

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 the

相关标签:
4条回答
  • 2020-12-09 05:52

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

    0 讨论(0)
  • 2020-12-09 05:54

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

    0 讨论(0)
  • 2020-12-09 05:58

    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.

    0 讨论(0)
  • 2020-12-09 06:00

    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
    
    0 讨论(0)
提交回复
热议问题