Albert Renshaw Albert Renshaw - 4 months ago 188x
iOS Question

Detect if a user has typed an emoji character in UITextView

I have a UITextView and I need to detect if a user enters an emoji character.

I would think that just checking the unicode value of the newest character would suffice but with the new emoji 2s, some characters are scattered all throughout the unicode index (i.e. Apple's newly designed copyright and register logos).

Perhaps something to do with checking the language of the character with NSLocale or LocalizedString values?

Does anyone know a good solution?



Edit!!!!!!!: --- The 55357 method does not work with ALL emojis. Some with lower unicode values return numbers lower than 55357. Use the array method beneath the 55357 method but be prepared to update this if Apple adds more emoji characters.

I found out by chance that checking the unichar value of an emoji 1 (*NOT an Emoji 2) character always returns 55357 (This may happen with things other than just emojis though... I checked some abstract keyboards that can be enabled on iOS though and they all still worked.)

code to prevent emojis from being typed.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if (!(([text isEqualToString:@""]))) {//not a backspace, else `characterAtIndex` will crash.
        unichar unicodevalue = [text characterAtIndex:0];
        if (unicodevalue == 55357) {
            return NO;

List of All Emoji Characters available on the Apple iPhone built in keyboard (as of Mar 19 2014... iOS 6) (*Note: Some characters are actually two characters combined, like the flags... if you are making an array programmatically you need to treat these as TWO characters otherwise you will get two unicode letters and not get an emoji flag!).. :

allEmojis = [[NSMutableArray alloc] initWithObjects:@"