3

I am trying to add emoji in UITextField with existing text.But I get only unicode in the textfield in place of emoji like \ue415. I have try the below code to set emoji in the textfield.

I have a view in which there are buttons with images of emojis. on the click of that emoji i am appending the respective unicode of that emoji to the text. but in that unicode is appended in string format and not the actual symbol.

If I try to set direct unicode in the textfield then it show the emoji but if i try to get the value from array then it is not showing the emoji.

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];

And the output is like :

strCode:\ue415 and mycode:

Can anyone help me with how can i get the strCode same as myCode ?

dgund
  • 3,459
  • 4
  • 39
  • 64
Mansi Panchal
  • 2,357
  • 18
  • 27

2 Answers2

2
Use like below it will work  

 NSString *myString = @"I am sad of him";
 myString = [myString stringByReplacingOccurrencesOfString:@"sad" withString:@"\ue415"];
 txtField.text = myString;
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
  • Thanks for the reply. But actually I have a view in which there are buttons with images of emojis. on the click of that emoji i am appending the respective unicode of that emoji to the text. but in that unicode is appended in string format and not the actual symbol. – Mansi Panchal Jun 02 '12 at 08:31
0

Interesting question.

you can not put \ character as a string. in your code 'mycode' takes the @"\ue415" in one character not as string. you can see it with

NSlog(@"%d",[mycode length]);

... it will show you 1 instead of 6.

if you can set -

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

any how in your code....this will give you the string what you want.

Thank You!!

TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • Thanks For The Reply.Actually I was getting the length 6 only.but now i got the solution. I was getting the array from the plist file and so that i was not getting the string instead of unicode. So I made an array of unicodes and got it like this : `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 Jun 04 '12 at 05:05