ios - 检查字符串中是否包含表情符号

标签 ios objective-c emoji emoticons sizewithfont

我得到一个字符串的文本大小

textSize = [[tempDict valueForKeyPath:@"caption.text"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode: NSLineBreakByWordWrapping];

我遇到的唯一问题是,如果字符串只包含表情符号,我的应用程序就会崩溃。有没有一种简单的方法来检查表情符号,或者我是否必须创建一个包含所有可能的表情符号的数组,然后使用它来检查它们?

错误:

-[NSNull sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0x3aa88a60


if ([tempDict valueForKeyPath:@"caption.text"]){
            NSLog(@"%@", [tempDict valueForKeyPath:@"caption"]);
            //Measure the message label box height
            textSize = [[tempDict valueForKeyPath:@"caption.text"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode: NSLineBreakByWordWrapping];
            int height = 320 + 20 + textSize.height;
            [cellHeight addObject:[NSNumber numberWithInt:height]];
}

最佳答案

试试这段代码:

- (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 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 - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f) {
                     returnValue = YES;
                 }
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3) {
                 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) {
                 returnValue = YES;
             }
         }
     }];

    return returnValue;
}

关于ios - 检查字符串中是否包含表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19886642/

相关文章:

ios - wireguard ios,路由 ip+net : sysctl: operation not supported

ios - 如何以编程方式正确初始化 ViewController? loadView() 与 init(nibName : nil, bundle :nil)

objective-c - 理解弱引用

objective-c - 如何在 Storyboard中设置初始 ViewController 的委托(delegate)

python - 在 python 中将表情符号 unicode 转换为文本

android - 如何在 Jetpack Compose 中显示 Emoji?

html - 为什么表情符号不能在 Chrome 中呈现超过特定大小?

ios - 更新查询对象上的解析 ACL

iOS Mystery crash w libsystem_kernel.dylib

ios - 尽管有标签和按钮,可重用的 UITableViewCells 显示为 null