有時候在開發(fā)時會遇到不希望字符串中包含emoji表情的情況奕锌,Google之后發(fā)現(xiàn)了方法,但是似乎iOS9之后的emoji無法過濾,于是繼續(xù)尋找方法阿蝶,在一個NSString的擴(kuò)展中發(fā)現(xiàn)了辦法,懶得導(dǎo)入了黄绩,就直接寫個方法吧羡洁。
//判斷是否有emoji
+(BOOL)stringContainsEmoji:(NSString *)string
{
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar high = [substring characterAtIndex: 0];
// Surrogate pair (U+1D000-1F9FF)
if (0xD800 <= high && high <= 0xDBFF) {
const unichar low = [substring characterAtIndex: 1];
const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
if (0x1D000 <= codepoint && codepoint <= 0x1F9FF){
returnValue = YES;
}
// Not surrogate pair (U+2100-27BF)
} else {
if (0x2100 <= high && high <= 0x27BF){
returnValue = YES;
}
}
}];
return returnValue;
}
ps:擴(kuò)展的鏈接是https://github.com/woxtu/NSString-RemoveEmoji 感謝作者