The problem is that
NSRange range=[self.textLabel.text rangeOfString:word];
finds the first occurrence of the word in the text. A better option is to enumerate the text by words:
-(void)setTextHighlited :(NSString *)txt{
NSString *text = self.textLabel.text;
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:text];
[text enumerateSubstringsInRange:NSMakeRange(0, [text length])
options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([substring isEqualToString:txt]) {
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:substringRange];
}
}];
self.textLabel.attributedText = string;
}
This method has more advantages, for example it will find a word even if it is enclosed in quotation marks or surrounded by punctuation.