在iOS中,可以使用NSString的方法来判断字符串是否包含emoji表情。以下是一个示例代码:

NSString *text = @"Hello 😃";
BOOL containsEmoji = NO;

[text enumerateSubstringsInRange:NSMakeRange(0, [text length])
                         options:NSStringEnumerationByComposedCharacterSequences
                      usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                          const unichar high = [substring characterAtIndex:0];
                          if (0xd800 <= high && high <= 0xdbff) {
                              const unichar low = [substring characterAtIndex:1];
                              const int codepoint = ((high - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
                              if (0x1d000 <= codepoint && codepoint <= 0x1f9ff){
                                  containsEmoji = YES;
                                  *stop = YES;
                              }
                          } else if (substring.length > 1) {
                              const unichar low = [substring characterAtIndex:1];
                              if (low == 0x20e3 || low == 0xfe0f || low == 0xd83c) {
                                  containsEmoji = YES;
                                  *stop = YES;
                              }
                          } else {
                              if (0x2100 <= high && high <= 0x27ff) {
                                  containsEmoji = YES;
                                  *stop = YES;
                              } else if (0x2B05 <= high && high <= 0x2b07) {
                                  containsEmoji = YES;
                                  *stop = YES;
                              } else if (0x2934 <= high && high <= 0x2935) {
                                  containsEmoji = YES;
                                  *stop = YES;
                              } else if (0x3297 <= high && high <= 0x3299) {
                                  containsEmoji = YES;
                                  *stop = YES;
                              } else if (high == 0xa9 || high == 0xae || high == 0x303d || high == 0x3030 || high == 0x2b55 || high == 0x2b1c || high == 0x2b1b || high == 0x2b50) {
                                  containsEmoji = YES;
                                  *stop = YES;
                              }
                          }
                      }];

if (containsEmoji) {
    NSLog(@"包含emoji表情");
} else {
    NSLog(@"不包含emoji表情");
}

这段代码会遍历字符串中的每个字符,然后检查字符是否属于emoji表情。如果字符串中包含emoji表情,则会设置containsEmoji为YES,否则为NO


原文地址: https://www.cveoy.top/t/topic/iMtq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录