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 judgecomposedString
What 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.