如何判断一个NSString里是否含有判断emoji表情情

Emoji in UILabel [在UILabel emoji] - 问题-字节技术
Emoji in UILabel
在UILabel emoji
问题 (Question)
Ive been trying to find a solution to my problem all day with no success.
I know its probably something silly since I am still new to OBjective C so heres my issue.
I'm trying to implement unicode Emoji Characters into a UILabel.
So lets take it one step at a time.
Here is the Json String I am using:
"body":"\\U0001f61f \\U0001f62e \\U"
As you can see the Json Serializer im using escapes the unicode.
I suspect this is my main problem.
I am retrieving the data like so:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *concat=[NSString stringWithFormat:@"MYURL?ID=%@",_TID];
NSURL *url = [NSURL URLWithString:concat];
NSData *jsonData=[NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
if (jsonData!=nil) {
newX= [[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]mutableCopy];
pageCounter=2;
[self.topicView reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[loadingView
performSelector:@selector(removeView)
withObject:nil
afterDelay:1];
After some time later the data is called and added to the UILabel.
UILabel *emo=(UILabel*)[Tcell viewWithTag:6];
NSString*body=[[[newX objectAtIndex:indexPath.row] valueForKey:@"body"] mutableCopy];
Which just gives me \U0001f61f \U0001f62e \U as text.
I know its probably just an encoding issue but I have no idea where to attack this.
Any help would be appreciated.
我一直在努力没有成功找到一个解决方案,一整天我的问题。我知道它可能是愚蠢的东西,因为我仍然新目标C,我的问题。我想成为一个Unicode字符UILabel实施emoji。所以让我们走一步看一步。这是我使用JSON字符串:"body":"\\U0001f61f \\U0001f62e \\U"
你可以看到JSON序列化IM使用转义Unicode。我怀疑这是我的主要问题。我喜欢这样的检索数据:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *concat=[NSString stringWithFormat:@"MYURL?ID=%@",_TID];
NSURL *url = [NSURL URLWithString:concat];
NSData *jsonData=[NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
if (jsonData!=nil) {
newX= [[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]mutableCopy];
pageCounter=2;
[self.topicView reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[loadingView
performSelector:@selector(removeView)
withObject:nil
afterDelay:1];
经过一段时间后,数据调用添加到UILabel。UILabel *emo=(UILabel*)[Tcell viewWithTag:6];
NSString*body=[[[newX objectAtIndex:indexPath.row] valueForKey:@"body"] mutableCopy];
这只是给了我u0001f61f \ u0001f62e \ u文本。我知道这可能是一个编码的问题,但我不知道攻击。任何帮助将不胜感激。:)
最佳答案 (Best Answer)
To display those characters you need to use a font in which they are valid. On iOS that means using the Apple Color Emoji font.
显示这些特点,你需要使用一个字体,它们是有效的。在iOS意味着使用Apple Color Emoji字体
答案 (Answer) 2
So after spending all day doing this the ONLY solution I found was to create an NSMutableDictionary and call the keys from there.
所以在一整天的做我唯一发现的是创建一个nsmutabledictionary从那里把钥匙。
答案 (Answer) 3
When your json string is "\U0001f61f" json understands \\ as an escape to represent a literal slash. You should only use a single slash if you want the "U" and hexadecimal digits themselves to be escaped.
When you create a string like this:
NSString *s = @"\U0001f61f \U0001f62e \U";
You get emoji characters because the Objective-C compiler understands and transforms the \U escapes (called Universal Character Names, from C and C++) to the appropriate characters in string literals. If you use a string from json, or a plist, or anything other than a string literal the escape sequence will not be processed at compile time by the Objective-C compiler.
Note: See the C or C++ language specifications to see how escapes in string literals are processed in Objective-C or Objective-C++.
Instead you want to create a string in json using escapes json understands. Unfortunately json is deficient in its Unicode support for characters outside the BMP (i.e., greater than U+FFFF). json has a \u escape, but it only supports four hexadecimal digits. To use this with characters outside the BMP there's a hack that involves using surrogate codepoints just as UTF-16 does.
The json string that encodes the characters you want is:
"body":"\uD83D\uDE1F \uD83D\uDE2E \uD83D\uDE21"
(If you use Google to search for a Unicode character using the format U+123456 it will find a page that tells you the UTF-16 encoding.)
Hopefully this is the only change you need to make, however I'm unsure if NSJSONSerialization supports this hack. If this doesn't work on its own you'll need to take a look at the documentation (assuming the behavior is documented) for how NSJSONSerialization produces string data from json data.
当你的JSON字符串”u0001f61f JSON理解”\\作为一种逃避代表文字的斜线。你应该只使用一个单斜杠如果你想要“U”和十六进制数字本身是逃跑的。当你创建一个字符串这样:NSString *s = @"\U0001f61f \U0001f62e \U";
你得到的绘文字字符因为Objective-C编译器了解和转化\U逃避(称为通用字符名称,从C和C + +)在适当的字符字符串。如果你使用一个字符串从JSON,或一个plist,或比其他任何一个字符串文字转义序列将不被处理在Objective-C编译器编译的时候。注:看到C或C++语言规范,看到在字符串中的转义是如何Objective-C或Objective-C++的处理。相反,你要创建JSON使用JSON字符串不理解。不幸的是JSON在外的BMP字符的Unicode支持不足(即,大于U+FFFF之间)。JSON有\u逃脱,但它仅支持四个十六进制数字。使用此外BMP字符有黑客会使用替代的码点就像UTF-16呢。你想要的字符编码,JSON字符串:"body":"\uD83D\uDE1F \uD83D\uDE2E \uD83D\uDE21"
(如果你使用谷歌搜索使用格式化U + 123456,它会找到一个页面告诉你UTF-16编码的Unicode字符。)希望这是你需要做的只是改变,但是我不知道NSJSONSerialization支持这个技巧。如果这没有对自己的工作,你需要在文档的外观(假设的行为是如何记录)NSJSONSerialization产生的字符串数据从JSON数据。
答案 (Answer) 4
I use the below code to encode emoji character
NSString *uniText = [NSString stringWithUTF8String:[textview.text UTF8String]];
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding] ;
And the below code to decode and display in UILabel
const char *jsonString = [body UTF8String];
NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
NSString *goodMsg = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
我用下面的代码进行编码的绘文字字符NSString * unitext = [ NSString stringwithutf8string:[ textview.text utf8string ] ]; NSData * msgdata = [ unitext datausingencoding:nsnonlossyasciistringencoding ]; NSString * goodmsg = [ ] [ NSString分配initwithdata:msgdata编码;和下面的代码进行解码并显示在UILabelconst char* jsonstring = [体utf8string ]; NSData * JSONData = [ NSData datawithbytes:jsonstring长度:strlen(jsonstring)]; NSString * goodmsg = [ ] [ NSString分配initwithdata:JSONData编码;
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:iOS&自定义emoji表情键盘总结
之前走了很多弯路,包括自己定以emoji表情,自己创建view类去处理图文混排 ,当把这些焦头烂额的东西处理完了才发现
,其实系统自带键盘是如此的方便,iOS
系统自带的表情在view,textfield,UIimageView展示时已经将uicode做过处理,直接展示成图片的形式,其实质依然是UTF8的
符号,如果你要自己定义图片,然后添加进textField 或者Label里,那你就中大奖了,图文混排 需要费很大的功夫处理。
首先 ,想要获取系统的表情,要首先知道表情对应的UTF8 的编码方式,我将其中一部分的图片展示出来 ,然后用UIButton
排列,iOS 7后又增加了300多个表情符号,这些都可以百度查到,现在上代码:
//将数字转为
#define EMOJI_CODE_TO_SYMBOL(x) ((((0x
| (x & 0x3F000)
| (x & 0xFC0)
| (x & 0x1C0000)
| (x & 0x3F)
定义的宏将转成UTF8,取出对应的表情符号:
//获取默认表情数组
- (NSArray
*)defaultEmoticons {
& NSMutableArray *array
= [NSMutableArray new];
i=0x1F600;
i&=0x1F64F;
(i & 0x1F641
|| i & 0x1F644)
sym = EMOJI_CODE_TO_SYMBOL(i);
& & NSString
= [[NSString
initWithBytes:&sym
length:sizeof(sym)
encoding:NSUTF8StringEncoding];
& & & [array
addObject:emoT];
& & return
在将对应数组里的表情依次存放到UIButton里,贴出部分代码:
& //获取数组
& & NSArray
*arrEmotion = [self
defaultEmoticons];
& //将表情放到UIButton里
& & CGFloat
& & CGFloat
& & CGFloat
& & CGFloat
& UIButton
*biaoqing =[[UIButton
& biaoqing.backgroundColor
= [UIColor
redColor];
& biaoqing.frame
= CGRectMake(X,
& [self.view
addSubview:biaoqing];
& NSString
*Str = arrEmotion[i];
[biaoqing setTitle:Str
forState:UIControlStateNormal];
& biaoqing.tag
[biaoqing addTarget:self
action:@selector(biaoqingClick:)
forControlEvents:UIControlEventTouchUpInside];
这样就完成了 在你自定义键盘里的表情添加
,同时设置该点击事件将表情符号添加进textField或者Label里:
biaoqingClick:(UIButton
*)biaoqing{
& & NSArray
*emoji = [self
defaultEmoticons];
& & NSString
*str = emoji[biaoqing.tag];
& & self.textField.text
基本功能完成 ,效果图展示:
<img NAME="image_operate_20765"
ALT="iOS&自定义emoji表情键盘总结"
TITLE="iOS&自定义emoji表情键盘总结" />
在后台服务器处理表情方面 ,mysql 5.4之后 就可以自动识别 uicode
的表情编码,并且经验证iOS端的表情符号在安卓上也可以识别,如果mysql的版本过低,报错无法识别uicode编码
,可以参见服务端对emoji支持的博客:/a/6820
未完 ,待续 &后续贴出做出类似 自定义表情的展示方式 。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。20764人阅读
iOS开发(8)
服务器端不支持Emoji表情,因此客户端在上传用户输入时,不能包含Emoji表情。
在客户端发送请求前,判断用户输入中是否含有表情,如果含有表情,则提示用户重新输入。这个过程关键是如何判断字符串中是否含有Emoji表情。要判断是否含有Emoji表情,必须先了解什么是Emoji。
Emoji 是一套起源于日本的12x12像素表情符号,由栗田穣崇(Shigetaka Kurit)创作,最早在日本网络及手机用户中流行,自苹果公司发布的iOS 5输入法中加入了emoji后,这种表情符号开始席卷全球,目前emoji已被大多数现代计算机系统所兼容的Unicode编码采纳,普遍应用于各种手机短信和社交网络中。
以上是摘自百度百科里的一段话,Emoji表情最终会被编码成Unicode,因此,只要知道Emoji表情的Unicode编码的范围,就可以判断用户是否输入了Emoji表情。以下是具体代码,我这里写成了NSString的一个分类
@implementation NSString (Emoji)
+ (BOOL)stringContainsEmoji:(NSString *)string
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 &= hs && hs &= 0xdbff) {
if (substring.length & 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 &= uc && uc &= 0x1f77f) {
returnValue = YES;
} else if (substring.length & 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
if (0x2100 &= hs && hs &= 0x27ff) {
returnValue = YES;
} else if (0x2B05 &= hs && hs &= 0x2b07) {
returnValue = YES;
} else if (0x2934 &= hs && hs &= 0x2935) {
returnValue = YES;
} else if (0x3297 &= hs && hs &= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
return returnV
这段代码并非本人原创,也是取自互联网,经测试好使,分享给大家。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:64687次
排名:千里之外
原创:10篇
评论:19条
(1)(2)(1)(3)(3)判断是不是是 emoji表情 - 移动开发当前位置:& &&&判断是不是是 emoji表情判断是不是是 emoji表情&&网友分享于:&&浏览:167次判断是否是 emoji表情
+ (BOOL)stringContainsEmoji:(NSString *)string
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 &= hs && hs &= 0xdbff) {
if (substring.length & 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 &= uc && uc &= 0x1f77f) {
returnValue = YES;
} else if (substring.length & 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
// non surrogate
if (0x2100 &= hs && hs &= 0x27ff) {
returnValue = YES;
} else if (0x2B05 &= hs && hs &= 0x2b07) {
returnValue = YES;
} else if (0x2934 &= hs && hs &= 0x2935) {
returnValue = YES;
} else if (0x3297 &= hs && hs &= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
return returnV
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有百度知道 - 信息提示
知道宝贝找不到问题了&_&!!
该问题可能已经失效。
秒以后自动返回

我要回帖

更多关于 js判断emoji表情 的文章

 

随机推荐