美文网首页
去除字符串的表情

去除字符串的表情

作者: guoguojianshu | 来源:发表于2018-09-04 14:35 被阅读55次

    //头文件#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

    相关文章

      网友评论

          本文标题:去除字符串的表情

          本文链接:https://www.haomeiwen.com/subject/okbgwftx.html