单个字符emoji转UTF-8
//emojiTab
+ (NSArray*)emojiReplaceTab{
NSArray * emojiArray = @[@"😀",@"😁",@"😂",@"😃",@"😄",@"😅",@"😆",@"😈",@"😉",@"😐",
@"😑",@"😒",@"😓",@"😔",@"😕",@"😖",@"😗",@"😘",@"😙",@"😠",
@"😡",@"😢",@"😣",@"😤",@"😥",@"😦",@"😧",@"😨",@"😩",@"😰",
@"😱",@"😲",@"😳",@"😴",@"😵",@"😶",@"😷",@"🙁",@"🙂",@"🙃",
@"🙄",@"🤐",@"🤑",@"🤒",@"🤓",@"🤔",@"🤕",@"🤗",@"😊",@"😋",
@"😌",@"😍",@"😎",@"😏",@"😚",@"😛",@"😜",@"😝",@"😞",@"😟",
@"😪",@"😫",@"😬",@"😭",@"😮",@"😯",@"☺",@"😇",@"👻",@"👿",
];
return emojiArray;
}
//单个字符emoji转UTF-8
+ (NSString*)emojiToUnicode:(NSString*)text{
#define MULITTHREEBYTEUTF16TOUNICODE(x,y) (((((x ^ 0xD800) << 2) | ((y ^ 0xDC00) >> 8)) << 8) | ((y ^ 0xDC00) & 0xFF)) + 0x10000
NSString *hexstr = @"";
NSArray * emojiArray = [NSString emojiReplaceTab];
//不在字符表 返回原值
NSInteger inx = [emojiArray indexOfObject:text];//]text];
if (inx == NSNotFound){
return text;
}
//转Unicode 开始
hexstr = @"";
if ([text length] >= 2) {
for (int i = 0; i < [text length] / 2 && ([text length] % 2 == 0) ; i++)
{
// three bytes
if (([text characterAtIndex:i*2] & 0xFF00) == 0 ) {
hexstr = [hexstr stringByAppendingFormat:@"Ox%1X 0x%1X",[text characterAtIndex:i*2],[text characterAtIndex:i*2+1]];
}
else
{// four bytes
// hexstr = [hexstr stringByAppendingFormat:@"U+%1X ",MULITTHREEBYTEUTF16TOUNICODE([text characterAtIndex:i*2],[text characterAtIndex:i*2+1])];
hexstr = [hexstr stringByAppendingFormat:@"U000%1X",MULITTHREEBYTEUTF16TOUNICODE([text characterAtIndex:i*2],[text characterAtIndex:i*2+1])];
}
}
// PKLog(@"(unicode) [%@]",hexstr);
}
else
{
// PKLog(@"(unicode) U+%1X",[text characterAtIndex:0]);
hexstr = text;
}
return hexstr;
}
emoji转编码
//将一串含emojy字符的字符串,里面的emoji转为编码字符输出
+ (NSString*)textWithEmojiToText:(NSString*)text{
NSString *str= text;//@"👻😄🇲🇵是q是打";
__block NSString* converText = @"";
[str enumerateSubstringsInRange:NSMakeRange(0,str.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
NSString *str = [NSString emojiToUnicode:substring];
converText = [converText stringByAppendingString:str];
}];
return converText;
}
emojy编码字符的字符串,里面的emoji编码转为字符
//将一串含emojy编码字符的字符串,里面的emoji编码转为字符
+ (NSString*)textWithEmojiCodingToText:(NSString*)text{
NSArray * emojiArray = [NSString emojiReplaceTab];
for (NSString * emjoy in emojiArray) {
NSString* codingText = [NSString emojiToUnicode:emjoy];
text = [text stringByReplacingOccurrencesOfString:codingText withString:emjoy];
}
return text;
}
字符串截取
NSArray *textArray = [text componentsSeparatedByString:@"\n"];