emoji not being displayed, weird characters instea

2019-07-01 11:40发布

问题:

I'm trying to display an emoji (ascii #55357) in CoreText and this is what I'm getting:

It displays two of these characters and inserts the cursor in the middle. I'm not going to include my CoreText code because it's such a mess and I have no idea where the bug is originating from, but what I was just curious has anyone else seen the same problem before in any facet of iOS and what is the meaning of these sort of A in a black and white box characters that got displayed? Does it mean invalid character or something? Just looking for some hints on how to even approach this problem.

回答1:

The problem was that I didn't realize an emoji character has a length of 2 and not 1 like most other characters. So what specifically was causing the issue was I was applying attributes to the emoji when adding it to my NSMutableAttributedString over a length 1 instead of 2. When I changed it to 2 it fixed it:

[self.text addAttributes: attributes range:NSMakeRange(self.cursor.position, 2 /* not 1 */)]