常常出現字符串帶有html標籤。下面有個方法一步到位去掉HTML標籤
- <span style="font-family: 'comic sans ms', sans-serif; color: #008080; font-size: medium;">+(NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim
- {
- NSScanner *theScanner = [NSScanner scannerWithString:html];
- NSString *text = nil;
-
- while ([theScanner isAtEnd] == NO) {
- // find start of tag
- [theScanner scanUpToString:@"<" intoString:NULL] ;
- // find end of tag
- [theScanner scanUpToString:@">" intoString:&text] ;
- // replace the found tag with a space
- //(you can filter multi-spaces out later if you wish)
- html = [html stringByReplacingOccurrencesOfString:
- [ NSString stringWithFormat:@"%@>", text]
- withString:@""];
- }
-
- return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
- }
- </span>
調用方法: html
notification33.alertBody =[self flattenHTML:body trimWhiteSpace:YES]; spa