去除字符串的表情

//头文件#import

@interface NSString (RemoveEmoji)

- (BOOL)isIncludingEmoji;

- (instancetype)removedEmojiString;

@end


#import "NSString+RemoveEmoji.h"

@implementation NSString (RemoveEmoji)

- (BOOL)isEmoji {

    const unichar high = [self characterAtIndex: 0];


    // Surrogate pair (U+1D000-1F77F)

    if (0xd800 <= high && high <= 0xdbff) {

        const unichar low = [self characterAtIndex: 1];

        const int codepoint = ((high - 0xd800) * 0x400) + (low - 0xdc00) + 0x10000;


        return (0x1d000 <= codepoint && codepoint <= 0x1f77f);


    // Not surrogate pair (U+2100-27BF)

    } else {

        return (0x2100 <= high && high <= 0x27bf);

    }

}

- (BOOL)isIncludingEmoji {

    BOOL __block result = NO;


    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])

                            options:NSStringEnumerationByComposedCharacterSequences

                          usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {

        if ([substring isEmoji]) {

            *stop = YES;

            result = YES;

        }

    }];


    return result;

}

- (instancetype)removedEmojiString {

    NSMutableString* __block buffer = [NSMutableString stringWithCapacity:[self length]];


    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])

                            options:NSStringEnumerationByComposedCharacterSequences

                          usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {

        [buffer appendString:([substring isEmoji])? @"": substring];

    }];


    return buffer;

}

@end

你可能感兴趣的:(去除字符串的表情)