How to DELETE one emoji in NSString

前端 未结 4 1218
鱼传尺愫
鱼传尺愫 2020-12-28 11:02

How to Traversal emojis in NSString There is a NSString method used to traversal substring of NSString

    NSString *text = @\"2012         


        
相关标签:
4条回答
  • 2020-12-28 11:23

    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!

    0 讨论(0)
  • 2020-12-28 11:28

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

    - (void)deleteButtonPressed:(id)sender
      {
         [self.textView deleteBackward];
      }
    
    0 讨论(0)
  • 2020-12-28 11:39

    Below is a solution based on Jesse Rusak's suggestion, however even the UITextInputTokenizer seems to be unable to properly parse the text, 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 11:48

    If someone encounters this in the modern era, know that the problem has been fixed in iOS 10 or earlier. The following Swift playground code now properly prints the flags (regional indicator symbol pairs):

    import Foundation
    
    var str = "2017                                                                    
    0 讨论(0)
提交回复
热议问题