How to remove one emulator in NSString

How to bypass emojis in NSString There is an NSString method used to bypass an NSString substring

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 the conclusion is?

Output:

  2- 0- 1- 2- 😊- ζˆ‘- 们- - 😊- 😊- 😊- 😊- 😊- πŸ‡·- πŸ‡Ί- πŸ‡°- πŸ‡·- πŸ‡―- πŸ‡΅- πŸ‡Ί- πŸ‡Έ- 

Instead

  2- 0- 1- 2- 😊- ζˆ‘- 们- - 😊- 😊- 😊- 😊- 😊- πŸ‡·πŸ‡Ί- πŸ‡°πŸ‡·- πŸ‡―πŸ‡΅- πŸ‡ΊπŸ‡Έ- 

Like the American flag πŸ‡ΊπŸ‡Έ, it consists of πŸ‡Ί and πŸ‡Έ The length of the flag appears to be 4, and they consist of two characters with a length of-2. while NSString is listed, it gives me πŸ‡Ί and πŸ‡Έ, not πŸ‡ΊπŸ‡Έ

When a string is in a UITextView, a BACKSPACE in the Keyboard is used, it can handle the deletion of emoji πŸ‡ΊπŸ‡Έ, not πŸ‡Έ.

In my application, I created a custom emoji keyboard, and there is a DELETE button. I want the DELETE button to work just like the BACKSPACE button on the keyboard.

Does anyone know how to handle this?

+4
source share
4 answers

I am not a Unicode expert, but I believe that the problem is that flags are actually two characters. They are regional symbols of indicators. For example, the American flag is two different characters: REGIONAL INDICATOR SYMBOL LETTER U and REGIONAL INDICATOR SYMBOL LETTER S

http://en.wikipedia.org/wiki/Regional_Indicator_Symbol

Perhaps you are lucky to ask the text input system to make a tokenization for you, because it seems to understand (for example, when deleting it) that they should be considered as a whole. Try using the UITextInputTokenizer positionFromPosition:toBoundary:inDirection: to go from the end of the line to the previous character (i.e. Pass UITextGranularityCharacter ). You can get the tokenizer from the UITextView or UITextArea tokenizer .

Hope this helps!

+2
source

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

 - (void)deleteButtonPressed:(id)sender { [self.textView deleteBackward]; } 
+13
source

The following is a solution based on Jesse Rusak's suggestion, however, even the UITextInputTokenizer seems to be unable to parse the text correctly, unless, of course, the error is on my part :)

 +(void)parse:(UITextField *)textfield { id<UITextInputTokenizer> tokenizer = [textfield tokenizer]; UITextPosition *pos = textfield.beginningOfDocument; while (true) { UITextRange *range = [tokenizer rangeEnclosingPosition:pos withGranularity:UITextGranularityCharacter inDirection:UITextStorageDirectionForward]; NSString *oneCharacter = [textfield textInRange:range]; NSLog( @"%@", oneCharacter ); pos = [textfield positionFromPosition:pos offset:-1]; if (pos == nil) break; } } 
+1
source

If someone is facing this in the modern era, be aware that the problem has been fixed in iOS 10 or earlier. The following Swift game code now correctly prints flags (regional indicator symbol pairs):

 import Foundation var str = "2017πŸ˜Šζˆ‘δ»¬πŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ‡ΊπŸ‡ΈπŸ‡·πŸ‡ΊπŸ‡°πŸ‡·πŸ‡―πŸ‡΅" str.enumerateSubstrings(in: str.startIndex..<str.endIndex, options: .byComposedCharacterSequences) { (substr, range, enclRange, stop) in print(substr!) } 
0
source

All Articles