简体   繁体   中英

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM