2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Julio Garcia
  • 665
  • 6
  • 17
  • For anyone coming to this in the future, I needed this and made [an open source project called NSCharacterSet+EmojiCharacterSet](https://github.com/matthewpalmer/NSCharacterSet-EmojiCharacterSet) – matthewpalmer May 08 '16 at 05:07

2 Answers2

6

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
rmaddy
  • 314,917
  • 42
  • 532
  • 579
2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267