I'm implementing a "search in chat" feature and I want the searched word to be highlighted in messages. The problem, as described in title, is that if the word is the first of a line (long messages are clearly multi-line), the entire line is highlighted instead of the single word.
While debugging, I also tried to apply an underline instead of backgroundcolor, and the underline is correct. I can't figure out where's the problem. My app's chat is based on JSQMessagesViewController so I was thinking that the problem could be there.
[attributedString addAttribute:NSBackgroundColorAttributeName
value:backColor
range:wordRange];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:wordRange];
cell.textView.attributedText = attributedString;
I don't post the code that calculates range because range is correct; in fact, if I look at attributedString's preview in debug (where I assign it to cell.textView.attributedText) I can see that only the word is highlighted, and not all the string.
Seems like a bug in the system framework, so probably best bet would be to add one more line of code, setting background color back to "unchanged", directly after the last character where background should be changed. Good luck!