iOS支援富文字Label控制元件
阿新 • • 發佈:2019-02-11
- (MTARichLabelTextMode *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim { NSScanner *theScanner = [NSScanner scannerWithString:html]; NSString *text = nil; NSMutableArray<MTARichStyleMode *> *richArray = [NSMutableArray new]; MTARichLabelTextMode *richLabelMode = [MTARichLabelTextMode new]; NSArray<NSString*> *tarArray = [self targetStr:html]; while ([theScanner isAtEnd] == NO) { MTARichStyleMode *stylemode = [MTARichStyleMode new]; // find start of tag [theScanner scanUpToString:@"<" intoString:NULL] ; // find end of tag [theScanner scanUpToString:@"/" intoString:&text] ; if([text containsString:@"color"]){ stylemode.color = [self tarStrHTML:text reg:@"['](.*)[']" repStr:@"'"]; } if([text containsString:@"<b"]){ stylemode.bold = YES; } text = nil; if(stylemode.color.length>0||stylemode.bold){ [richArray addObject:stylemode]; } } for (NSUInteger i =0; i< tarArray.count; i++) { richArray[i].target = tarArray[i]; } richLabelMode.desc = [self getDesc:html]; richLabelMode.styleArray = richArray; return richLabelMode; } //獲取富文字的目標文字 - (NSArray<NSString*> *)targetStr:(NSString *)html { NSScanner *theScanner = [NSScanner scannerWithString:html]; NSString *text = nil; NSMutableArray<NSString *> *tarArray = [NSMutableArray new]; 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) if([text containsString:@">"] && [text containsString:@"<"]){ text = [self getSynax:text]; if(text!=nil){ text = [text stringByReplacingOccurrencesOfString:@">" withString:@""]; if(text.length>0) [tarArray addObject:text]; } } text = nil; } return tarArray; } //去掉多餘標籤 - (NSString *)getSynax:(NSString *) html { 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] ; if(text!=nil){ text = [text stringByReplacingOccurrencesOfString:@">" withString:@""]; } } return text; } //過濾掉富文字標籤返回一個純文字 - (NSString *)getDesc:(NSString *) html { 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] ; html = [html stringByReplacingOccurrencesOfString: [ NSString stringWithFormat:@"%@>", text] withString:@""]; } return [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } - (NSString *)tarStrHTML:(NSString *)html reg:(NSString *)regStr repStr:(NSString*) rpStr { NSString *result = nil; NSRange range = [html rangeOfString:regStr options:NSRegularExpressionSearch]; if (range.location != NSNotFound) { NSLog(@"%@", [html substringWithRange:range]); result = [html substringWithRange:range]; result = [result stringByReplacingOccurrencesOfString:rpStr withString:@""]; } return result; }
設定到Label中: