Filter Emoji in NSString and NSStringEmoji

Source: Internet
Author: User

Filter Emoji in NSString and NSStringEmoji

Sometimes, because of project requirements, you need to filter emoji in NSString.
For example:

  • To communicate with android, android does not support this function yet.
  • Content is sent as a text message.

The idea is as follows: traverse each NSString character and replace it with another character. Print each character:

NSRange range;    for(NSInteger i = 0; i < length; i += range.length) {        range = [string rangeOfComposedCharacterSequenceAtIndex:i];        NSString *composedString = [string substringWithRange:range];        NSLog(@"i = %@, ch = %@", @(i), composedString);    }`

How to judgecomposedStringWhat about emoji?

  • Method 1: find it in the emoji dictionary. It is not ideal to load a comprehensive emoji.
  • Method 2: whether it is a normal character, that is, it is not emoji.

Determine whether the current Code point is emoji.

- (BOOL)isNotEmoji:(UInt64) codePoint {    return (codePoint == 0x0)    || (codePoint == 0x9)    || (codePoint == 0xA)    || (codePoint == 0xD)    || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))    || ((codePoint >= 0xFF00) && (codePoint <=                                  0xFFFF));}

Based on the above two methods, I wrote an NSString extension class:
NSString + YFEmoji

This article is original. For more information, see.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.