如何在UIlabel和UItextviews中显示表情符号和特殊字符? [英] How to display the emoji and special characters in UIlabel and UItextviews?

查看:723
本文介绍了如何在UIlabel和UItextviews中显示表情符号和特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在各种项目中显示字符串,例如UIlabel,UItextview,Uitextfield等......我试图以这样的方式做这样的事情

I am trying to display a string in all sorts of items such as UIlabel,UItextview,Uitextfield etc.....I am trying to do like this in a manner like this

NSData *data1 = [title dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data1 encoding:NSNonLossyASCIIStringEncoding];
label.text=goodvalue;

这对我来说有时会工作,但有时会为这个字符串返回null Youtube\\\�\\\�\\\�\\\�\\\�\\\�\\\�。任何人都可以指导我吗?

this is working sometimes for me ,but some times it returns null for the string like this "Youtube\ud83d\ude27\ud83d\ude2e\ud83d\ude2f\ud83d".Can anybody guide me on this?

推荐答案

表情符号字符位于unicode平面1中,因此需要16位以上才能表示代码点。因此,两个UTF8表示或一个UTF32表示。 Unicode实际上是一个21位系统,对于平面0字符(基本上除表情符号之外的所有东西)16位就足够了,我们可以使用16位。表情符号需要超过16位。

Emoji characters are in unicode plane 1 and thus require more than 16 bits to represent a code point. Thus two UTF8 representations or one UTF32 representation. Unicode is actually a 21-bit system and for plane 0 characters (basically everything except emoji) 16 bits is sufficient and we get by using 16 bits. Emoji need more than 16 bits.

Youtube\\\�\\\�\\\�\\\�\\\�\\\�\ ud83d。无效,它是utf16 unicode转义字符串的一部分,最后 \\\� 是表情符号字符的1/2。

"Youtube\ud83d\ude27\ud83d\ude2e\ud83d\ude2f\ud83d". is invalid, it is part of a utf16 unicode escaped string, the last \ud83d is 1/2 of an emoji character.

此外,为了使用转义字符\创建文字字符串,必须转义转义字符:\\。

Also, inorder to create a literal string with the escape character "\" the escape character must be escaped: "\\".

NSString *emojiEscaped = @"Youtube\\ud83d\\ude27\\ud83d\\ude2e\\ud83d\\ude2f";
NSData *emojiData = [emojiEscaped dataUsingEncoding:NSUTF8StringEncoding];
NSString *emojiString = [[NSString alloc] initWithData:emojiData encoding:NSNonLossyASCIIStringEncoding];
NSLog(@"emojiString: %@", emojiString);

NSLog输出:

这篇关于如何在UIlabel和UItextviews中显示表情符号和特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆