Reputation: 734
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.
Upvotes: 2
Views: 1682
Reputation: 38667
Swift equivalent of rmaddy's answer to add up ranges in a CharacterSet
:
var aCharacterSet = CharacterSet()
aCharacterSet.insert(charactersIn: "\u{1F300}"..<"\u{1F700}") // Add most of the Emoji characters
aCharacterSet.insert(charactersIn: "A"..."Z") // Add uppercase
aCharacterSet.insert(charactersIn: "a"..."z") // Add lowercase
Also, you will find the complete list of Unicode ranges for emoji on http://www.unicode.org/charts/ under Emoji & Pictographs.
Upvotes: 4
Reputation: 318824
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
Upvotes: 6