iosemojinscharacterset

IOS NSCharacterSet for emoji and latin characters


I'm trying to limit text user input to latin/english characters and emojis.

Is it possible to create an NSCharacterSet that includes all of these characters?

I tried using a keyboard type ASCIICapable on my input views, but then I don't get emoji input.


Solution

  • There's nothing built in to create such a specific character set. You'll have to do it yourself by character range.

    The Emoji characters are essentially in the range \U1F300 - \U1F6FF. I suppose a few others are scattered about.

    Use an NSMutableCharacterSet to build up what you need.

    NSMutableCharacterSet *aCharacterSet = [[NSMutableCharacterSet alloc] init];
    [aCharacterSet addCharactersInRange:NSMakeRange(0x1F300, 0x1F700 - 0x1F300)]; // Add most of the Emoji characters
    [aCharacterSet addCharactersInRange:NSMakeRange('A', 'Z'-'A'+1)]; // Add uppercase
    [aCharacterSet addCharactersInRange:NSMakeRange('a', 'z'-'a'+1)]; // Add lowercase