Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DELETE one emoji in NSString

Tags:

ios

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 ?

like image 299
aelam Avatar asked Jul 31 '12 23:07

aelam


2 Answers

To delete the last character in a UITextView, just call -deleteBackward

- (void)deleteButtonPressed:(id)sender
  {
     [self.textView deleteBackward];
  }
like image 124
Elf Sundae Avatar answered Nov 15 '22 09:11

Elf Sundae


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!

like image 2
Jesse Rusak Avatar answered Nov 15 '22 10:11

Jesse Rusak