去掉NSString中的HTML标签

经常出现字符串带有html标签。下面有个方法一步到位去掉HTML标签

  1. <span style="font-family: 'comic sans ms', sans-serif; color: #008080; font-size: medium;">+(NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim  
  2. {  
  3.     NSScanner *theScanner = [NSScanner scannerWithString:html];  
  4.     NSString *text = nil;  
  5.       
  6.     while ([theScanner isAtEnd] == NO) {  
  7.         // find start of tag  
  8.         [theScanner scanUpToString:@"<" intoString:NULL] ;  
  9.         // find end of tag  
  10.         [theScanner scanUpToString:@">" intoString:&text] ;  
  11.         // replace the found tag with a space  
  12.         //(you can filter multi-spaces out later if you wish)  
  13.         html = [html stringByReplacingOccurrencesOfString:  
  14.                 [ NSString stringWithFormat:@"%@>", text]  
  15.                                                withString:@""];  
  16.     }  
  17.       
  18.     return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;  
  19. }  
  20. </span>  

 调用方法:

 

    notification33.alertBody =[self flattenHTML:body trimWhiteSpace:YES];

 


原文地址:https://www.cnblogs.com/dyllove98/p/3249207.html