更新时间:2023-02-19 16:32:55
你可以通过使用NSScanner类解析html来完成。
- (NSString *)flattenHTML:(NSString *)html {
NSScanner * theScanner;
NSString * text = nil;
theScanner = [NSScanner scannerWithString:html]; ([theScanner isAtEnd] == NO){
[theScanner scanUpToString:@< intoString:NULL];
[theScanner scanUpToString:@> intoString:& text];
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@%@>,text] withString:@];
}
//
html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
返回html;
}
希望这有助于您。
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?
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.