First you should try to get all the ranges in the row, and then set the attribute for each range. There are some great code examples on stackoverflow: fooobar.com/questions/910497 / ...
Edit:
Here is an example for you
- (NSArray *)rangesOfString:(NSString *)searchString inString:(NSString *)str { NSMutableArray *results = [NSMutableArray array]; 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; }
Using:
NSArray *results = [self rangesOfString:@"foo" inString:@"foo bar foo"]; NSLog(@"%@", results);
gives you
( "NSRange: {0, 3}", "NSRange: {8, 3}" )
source share