Showing emoji in a UILabel?

泄露秘密 提交于 2019-12-29 03:05:08

问题


Not sure what I'm missing here, and searching hasn't helped me. I want to display emoji characters in a UILabel, and this isn't doing it:

UILabel *label = [[UILabel alloc] init];
label.font = [UIFont fontWithName:@"AppleColorEmoji" size:16.0];
label.text = [NSString stringWithFormat:@"%C", 0x1F431];
// ... etc.

Works fine with other non-letter unicode characters, e.g. chess pieces, but not with any emoji characters that I have tried.


回答1:


You are probably not using the correct encoding for your emoji characters. For instance in your example I think you are looking for something like this:

label.text = [NSString stringWithFormat:@"%C", 0xe04f];

Have a look at this table to get the encodings you need.




回答2:


To use Emoji's just press Control+command+space (⌃⌘Space). No need of using unicode for emoji.




回答3:


In xcode just go to the top bar and click EDIT > EMOJIS & SYMBOLS and an emoji box will pop up and you can literally add it to any text in the app, even works in the interface builder if you need to add it to the text of a uilabel there.




回答4:


In Swift you can do:

label.text = "🐈"

Be sure to include the quotes. Otherwise you'll be setting the text to whatever is in the variable. The following would display as "cat":

let 🐈 = "cat"
label.text = 🐈



回答5:


The unicode 6.1 encodings work as well, but you would have to specify them like this:

label.text = @"\U0001F431";


来源:https://stackoverflow.com/questions/11619267/showing-emoji-in-a-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!