第一步:找出高亮部分文字在整个文字中的位置:
- (NSArray *)rangesOfString:(NSString *)searchString inString:(NSString *)str {
NSMutableArray *results = [[NSMutableArray alloc] init];
NSRange searchRange = NSMakeRange(0, [str length]);
NSRange range;
while ((range = [str rangeOfString:searchString options:0 range:searchRange]).location != NSNotFound) {
[results addObject:[NSValue valueWithRange:range]];
searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
}
return results;
}
第二部:UILabel显示:
BussData * informationData = self.dataArray[indexPath.row];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:informationData.bussName];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];