項目需要對用戶輸入的emoji表情進行過濾,之前針對系統的emoji表情都是用unicode編碼集判斷的,但是隨著iOS系統的emoji表情越來越豐富,之前的判斷就顯得無力了,再加上第三方搜狗和百度輸入法的摻合,甚至是不同iOS系統的 API判斷都不一樣,所以有必要重新更新一下判斷的方法了。
經過分析得出,在iOS14和iOS12系統上,針對
if (![[textView textInputMode] primaryLanguage] || [[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] ) {
return NO;
}
這個api返回的結果不一致,iOS12返回的為emoji而iOS14的為zh-Hans。
結合搜狗輸入法和iOS系統鍵盤進行分析得出。在搜狗輸入法中輸入的emoji字元串都是2個長度,而iOS系統的有的甚至達到了11個長度(採用了字元組合),因此當針對長度為小於等於2的時候用一下方法:
if(string.length <= 2){
NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:string];
return isMatch;
}
這種針對非組合emoji判斷經過大量驗證可以滿足條件。
搜索輸入法和百度輸入法有個特殊功能,點擊符號-網路的時候可以髮長文本,例如:https:// www 等等。但是發現真正的emoji轉成unicode字元的時候都是非常有規律的,比如:
\u0020\u007E,每個表情符號由6個uncode字元組成。本來可以用正則進行字元串匹配,但是無奈不大會啊!下面貼上完整代碼。
完整方法
/**是否第三方的表情*/
+(BOOL)hasEmoji:(NSString*)string
{
NSString *other = @"➋➌➍➎➏➐➑➒"; //蘋果的系統九宮格的特殊字元
if([other rangeOfString:string].location != NSNotFound){
return NO;
}
NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:string];
if(isMatch){
return YES;
}
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0dxd800) * 0x400) + (ls - 0xdc00) + 0x10000;
//129500-129503 新發現的一些表情符號
//129305-129342
//129402-129499
if( (0x1d000 <= uc && uc <= 0x1f77f) ||
(129305 <= uc && uc <= 129342) ||
(129402 <= uc && uc <= 129499) ||
(129500 <= uc && uc <= 129503)){
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3 || ls == 0xfe0f || ls == 0xd83c || ls == 0xfe0e) {
returnValue = YES;
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0xfe45 || hs == 0xfe46) {
returnValue = YES;
}
}
}];
return returnValue;
}
再次測試後沒有發現任何問題,希望小夥伴測試的時候發現有問題記得留言call我。謝謝!!!
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/212453.html