Ive written a method to highlight a word within a paragraph by sending it an NSString
of that word, it was working perfectly until i faced this scenario:
When i have this text:
Their mother has tried dressing them in other ...
When I'm passing the word other
, the word "mother
" is being highlighted, also when I'm passing in
i got "dressin
g".
Here is my code:
-(void)setTextHighlited :(NSString *)txt{
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.textLabel.text];
for (NSString *word in [self.textLabel.text componentsSeparatedByString:@" "]) {
if ([word hasPrefix:txt]) {
NSRange range=[self.textLabel.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
}
I've tried to use rangeOfString:options: with all of its options, but still have the same problem.
Please advice