Skip to content

Instantly share code, notes, and snippets.

@PoomSmart
Last active Aug 20, 2021
Embed
What would you like to do?
Detect if string contains emoji (Using Objective-C and Swift)
#import <UIKit/UIKit.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreText/CoreText.h>
@interface EmojiUtilities : NSObject
+ (CFMutableCharacterSetRef)emojiCharacterSet;
+ (BOOL)containsEmoji:(NSString *)emoji;
@end
@implementation EmojiUtilities
+ (CFMutableCharacterSetRef)emojiCharacterSet {
static CFMutableCharacterSetRef set = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
set = CFCharacterSetCreateMutableCopy(kCFAllocatorDefault, CTFontCopyCharacterSet(CTFontCreateWithName(CFSTR("AppleColorEmoji"), 0.0, NULL)));
CFCharacterSetRemoveCharactersInString(set, CFSTR(" 0123456789#*"));
});
return set;
}
+ (BOOL)containsEmoji:(NSString *)emoji {
return CFStringFindCharacterFromSet((CFStringRef)emoji, [self emojiCharacterSet], CFRangeMake(0, emoji.length), 0, NULL);
}
@end
import UIKit
import CoreFoundation
import CoreGraphics
import CoreText
class EmojiUtilities {
static var emojiCharacterSet = EmojiUtilities.getCharacterSet()
class func getCharacterSet() -> CFMutableCharacterSet? {
let nullPtr: UnsafePointer<CGAffineTransform>? = nil
let set = CFCharacterSetCreateMutableCopy(kCFAllocatorDefault, CTFontCopyCharacterSet(CTFontCreateWithName("AppleColorEmoji" as CFString, 0.0, nullPtr)))
CFCharacterSetRemoveCharactersInString(set, " 0123456789#*" as CFString);
return set
}
class func containsEmoji(emoji: String) -> Bool {
let nullPtr: UnsafeMutablePointer<CFRange>? = nil
return CFStringFindCharacterFromSet(emoji as CFString, emojiCharacterSet, CFRangeMake(0, CFStringGetLength(emoji as CFString)), CFStringCompareFlags(rawValue: 0), nullPtr);
}
}
// Testing
EmojiUtilities.containsEmoji(emoji: "Hi 👩‍🌾")
EmojiUtilities.containsEmoji(emoji: "👩🏻‍💻")
@ctjalsma
Copy link

ctjalsma commented May 3, 2019

Elegant. But, seems to identify a space as an emoji.

CFCharacterSetRef emojiSetWithSpace = CTFontCopyCharacterSet(CTFontCreateWithName(CFSTR("AppleColorEmoji"), 0.0, NULL));
CFMutableCharacterSetRef emojiCharacterSet = CFCharacterSetCreateMutableCopy(NULL, emojiSetWithSpace); CFCharacterSetRemoveCharactersInString(emojiCharacterSet, (CFStringRef)@" "); // And whatever other characters you don't want to identified as emoji

Loading

@ed8009
Copy link

ed8009 commented Jul 25, 2019

Elegant. But, seems to identify a space as an emoji.

CFCharacterSetRef emojiSetWithSpace = CTFontCopyCharacterSet(CTFontCreateWithName(CFSTR("AppleColorEmoji"), 0.0, NULL));
CFMutableCharacterSetRef emojiCharacterSet = CFCharacterSetCreateMutableCopy(NULL, emojiSetWithSpace); CFCharacterSetRemoveCharactersInString(emojiCharacterSet, (CFStringRef)@" "); // And whatever other characters you don't want to identified as emoji

If you hadn’t written, I wouldn’t have noticed, thanks!
Best to add more characters:
CFCharacterSetRemoveCharactersInString(emojiCharacterSet, (CFStringRef)@" 0123456789#*");

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment