如何将NSString HTML标记转换为纯文本NSString?

Fra*_*s84 12 html iphone objective-c nsstring ios

一直在网上搜索如何将HTML字符串标记转换为纯文本的示例.

我从包含的Feed中获取信息HTML,然后在文本视图中显示此信息.是否UITextView有转换属性HTML或我必须在代码中执行此操作.我试过了:

NSString *str = [NSString stringWithCString:self.fullText encoding:NSUTF8StringEndcoding];
Run Code Online (Sandbox Code Playgroud)

但似乎没有用.有人有任何想法吗?

Mad*_*dav 33

你可以通过使用NSScanner类解析html来实现

- (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;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Vee*_*Raj 8

如果您使用的是UIWebView,那么将HTML解析为文本会更容易:

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
Run Code Online (Sandbox Code Playgroud)