How to Traversal emojis in NSString There is a NSString method used to traversal substring of NSString
NSString *text = @"2012πζ们 ππππππΊπΈπ·πΊπ°π·π―π΅";
[text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
printf("%s- ",[substring UTF8String]);
}];
Guess what is the output?
The output is :
2- 0- 1- 2- π- ζ- 们- - π- π- π- π- π- π·- πΊ- π°- π·- π―- π΅- πΊ- πΈ-
Rather than:
2- 0- 1- 2- π- ζ- 们- - π- π- π- π- π- π·πΊ- π°π·- π―π΅- πΊπΈ-
Like the American Flag πΊπΈ , it's composed by πΊ and πΈ The length of a flag seems to be 4, and they are composed by two length-2-composed-character. while the NSString is enumerated , it gives me πΊ and πΈ rather than πΊπΈ
When the string is in UITextView, the BACKSPACE in Keyboard is tapped, it can handle to delete the emoji πΊπΈ rather than πΈ.
In my app, I made a custom emoji keyboard and there is a DELETE button. I want to the DELETE button works just like the system BACKSPACE button in keyboard.
Does anyone know how to handle it ?
To delete the last character in a UITextView, just call -deleteBackward
- (void)deleteButtonPressed:(id)sender
{
[self.textView deleteBackward];
}
I'm not a unicode expert, but I believe the problem here is that the flags are actually two characters. They're regional indicator symbols. The american flag, for example, is two distinct characters: REGIONAL INDICATOR SYMBOL LETTER U
and REGIONAL INDICATOR SYMBOL LETTER S
.
http://en.wikipedia.org/wiki/Regional_Indicator_Symbol
You might have luck asking the text input system to do the tokenization for you, as it seems to understand (when deleting, for example) that they should be treated as one unit. Try using the UITextInputTokenizer
method positionFromPosition:toBoundary:inDirection:
to move from the end of the string to the previous character (ie pass UITextGranularityCharacter
). You can get a tokenizer from a UITextView or UITextArea's tokenizer
method.
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With