I color some parts of the text coming from the API (think "@mention", like on Twitter) using NSAttributedString.
The API gives me text and an array of objects representing parts of the text that are mentioned (or links, tags, etc.) that need to be colored.
But sometimes the coloring is shifted due to emojis.
For example, using this text:
"@ericd Some text. @apero"
API gives:
[{"text": "ericd", "len": 6, "pos": 0}, {"text": "apero", "len": 6, "pos": 18}]
which I successfully translated to NSAttributedString using NSRange:
for m in entities.mentions { let r = NSMakeRange(m.pos, m.len) myAttributedString.addAttribute(NSForegroundColorAttributeName, value: someValue, range: r) }
We see that "pos": 18
true, this is where "@apero" begins. The colored parts of "@ericd" and "@apero" are as expected.
but when some specific emojis combinations are used in the text, the API does not tolerate NSATtributedString well, the color is biased :
"@ericd Some texts. πΊβπ» @apero"
gives:
[{"text": "ericd", "len": 6, "pos": 0}, {"text": "apero", "len": 6, "pos": 22}]
"pos": 22
: The author of the API claims that this is correct, and I understand their point of view.
Unfortunately NSAttributedString does not agree, my coloring is disabled:
![enter image description here](https://fooobar.com/undefined)
The last characters for the second mention are not colored (because "pos" is too short due to emosis?).
As you may have guessed, I canβt change the way the API behaves, I need to adapt it on the client side.
Besides ... I have no idea what to do. Should I try to determine what emotions are in the text and manually change the position of the references when there are problematic emojis? But what would be the criteria for determining which emoji shifts the position and what does not? And how to decide how much bias I need? Maybe the problem is caused by NSAttributedString?
I understand that this is due to the length of emojis, once compiled in comparison with their length in the form of discrete characters, but ... well ... I got lost (sigh).
Please note that I tried to implement a solution similar to this stuff , because my API is compatible with this, but it worked only partially, some emojis still broke indexes:
![enter image description here](https://fooobar.com/undefined)