在UITextField中显示表情符号

3
我正在尝试在现有文本中添加表情符号到UITextField中。但是我在文本框中只得到了Unicode而不是表情符号,例如\ue415。我尝试使用以下代码设置表情符号到文本框中。
我有一个视图,在其中有带有表情符号图片的按钮。单击该表情符号后,我会将相应的表情符号Unicode附加到文本中。但是在字符串格式中附加了Unicode而不是实际符号。
如果我尝试直接在文本框中设置Unicode,则会显示表情符号,但是如果我尝试从数组中获取值,则不会显示表情符号。
NSString *strCode = [[arrPlist objectAtIndex:[sender tag]]objectForKey:@"UTFCode"];
strCode = [strCode stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSString *mycode = @"\ue415";
if ([mycode isEqualToString:strCode]) 
 {
     NSLog(@"both are same");
 }
NSLog(@"%@",[NSString stringWithFormat:@"strCode:%@ and mycode:%@",strCode,mycode]);
messageField.text = [messageField.text stringByAppendingFormat:@"%@ %@",strCode,mycode];

输出结果如下:

strCode:\ue415 和 mycode:

有人可以帮我想办法让strCode与myCode相同吗?


我认为表情符号图标不会在控制台窗口中显示。 - TheTiger
请访问此链接,那里有您的答案..!!! - NiravPatel
2个回答

2
Use like below it will work  

 NSString *myString = @"I am sad of him";
 myString = [myString stringByReplacingOccurrencesOfString:@"sad" withString:@"\ue415"];
 txtField.text = myString;

谢谢回复。实际上,我有一个视图,其中有带有表情符号图像的按钮。 在单击该表情符号时,我将相应的表情符号的Unicode附加到文本中。但是,该Unicode以字符串格式附加,而不是实际符号。 - Mansi Panchal

0

有趣的问题。

你不能将\字符作为字符串输入代码中。在你的代码中,'mycode'将@"\ue415"视为一个字符而不是字符串。你可以用

查看它。
NSlog(@"%d",[mycode length]);

...它会显示1而不是6。

如果你可以设置 -

NSString *mycode = [[NSString stringWithFormat:@"\\"]stringByAppendingFormat:@"ue415"]); 

在你的代码中,无论如何......这将给你想要的字符串。

谢谢!


谢谢回复。实际上我只得到了长度为6,但现在我找到了解决方法。我从plist文件中获取了数组,所以我没有得到字符串而是Unicode。 因此,我创建了一个Unicode数组,并像这样获取它:`arrEmojis = [[NSArray alloc]initWithObjects:@"\ue415",@"\ue056",@"\ue057",@"\ue414",@"\ue405",@"\ue106",@"\ue418",@"\ue417",@"\ue40d",@"\ue40a",@"\ue404",@"\ue105", nil];NSString *strCode = [arrEmojis objectAtIndex:[sender tag]];messageField.text = [messageField.text stringByAppendingFormat:@"%@",strCode];` - Mansi Panchal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接