我正在开发聊天应用。我想添加笑脸支持。我可以使用像\ue415
之类的unicode在iOS中直接显示笑脸:-)。当我使用此unicode字符串发送消息时,其他方面正在接收像
这样的方框。如何将unicode字符转换为nsstring。
我在集合视图中使用情感如bellow
代码:
data = [NSArray arrayWithObjects:@"\ue415",@"\ue056",@"\ue057",@"\ue414",@"\ue405",@"\ue106",@"\ue418",@"\ue417",@"\ue40d",@"\ue40a",@"\ue404",@"\ue105",@"\ue409",@"\ue40e",@"\ue402",@"\ue108",@"\ue403",@"\ue058",@"\ue407",@"\ue401",@"\ue40f",@"\ue40b",@"\ue406",@"\ue413",@"\ue411",@"\ue412",@"\ue410",@"\ue107",@"\ue059",@"\ue416",@"\ue408",@"\ue40c",@"\ue11a",@"\ue10c",@"\ue32c",@"\ue32a",@"\ue32d",@"\ue328",@"\ue32b",@"\ue022",@"\ue023",nil];
CollectionView委派方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"EmojiCell";
UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
UILabel *lable = (UILabel *)[cell viewWithTag:100];
lable.text = [data objectAtIndex:indexPath.row];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
messageField.text = [NSString stringWithFormat:@"%@%@",messageField.text,[data objectAtIndex:indexPath.row]];
}
发送消息的代码
NSString *messageStr = self.messageField.text ;
if([messageStr length] > 0) {
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue: messageStr];
XMPPJID *jid = [XMPPJID jidWithString:chatWithUser];
NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:[jid full]];
[message addChild:body];
[self.xmppStream sendElement:message];
}
答案 0 :(得分:0)
只需更改symbolCode。
unichar symbolCode = 0xf086;
NSString *chatIcon =[NSString stringWithCharacters:&symbolCode length:1];
为您编辑:
unichar symbolCode = 0xE415;
NSString *smile =[NSString stringWithCharacters:&symbolCode length:1];
NSLog(@"%@",smile);
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 100, 50)];
lab.text = smile;
[self.view addSubview:lab];