关于ios中禁用emoje表情的探索

2018-03-23  本文已影响0人  best_su

开发中的很多情况都会遇到文本中不允许有emoje的存在,或者一些后台就TMD根本就不支持emoje。你该这么办:

话不多说直接上代码:

创建个文件就ok,不论你是oc还是swift均可以使用。类方法简单、粗暴、实用。
//这是.m文件
#import "ContainEmojeData.h"

@implementation ContainEmojeData

+ (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];
         NSLog(@"%hu", hs);
         
         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 <= 0x1ffff) {
                     returnValue = YES;
                 }
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0xfe0f) {
                 returnValue = YES;
             }
         } else {
             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;
}

@end

//这是.h文件
#import <Foundation/Foundation.h>

@interface ContainEmojeData : NSObject

#pragma mark - 是否包含Emoje表情(如果包含返回YES)

/**
 判断是否包含Emoje表情

 @param string 需要查询的字符串
 @return 是否包含Emoje表情,yes表示包含,no表示不包含
 */
+ (BOOL)stringContainsEmoji:(NSString *)string;


@end
上一篇下一篇

猜你喜欢

热点阅读