25

Here is a pretty good article that references iOS emoticons and their code. For example \ue008 for the small camera.

I tried this in my code :

var myText: String = "\ue008"

This is not accepted by Xcode. How to include it ?

4
  • Ue008 is in the private use area. So there's not much you can expect. Commented Sep 10, 2015 at 15:58
  • That "pretty good article" seems to be utter rubbish. Use "Character Viewer" on the Mac to get correct Unicode values. Commented Sep 10, 2015 at 16:00
  • ditto gnasher729 - pretty rubbish article. Commented Jun 14, 2016 at 15:24
  • answered here with the use of UnicodeScalar function and with a simple example Commented Dec 5, 2017 at 12:04

9 Answers 9

53

If I understand what you are trying to achieve, then:

Press "ctrl + cmd + space" while in XCode. A sample usage of 'hearts' emoticon

cell.textLabel?.text = "❤️" + " \(liker) liked \(userBeingliked)'s photo"
Sign up to request clarification or add additional context in comments.

1 Comment

that is keyboard shortcut can be used OSX-wise everywhere.
21

That's from swift documentation:

let dollarSign = "\u{24}"        // $,  Unicode scalar U+0024
let blackHeart = "\u{2665}"      // ♥,  Unicode scalar U+2665
let sparklingHeart = "\u{1F496}" // 💖, Unicode scalar U+1F496

2 Comments

can you please share me that swift documentation link @Greg
11

You don't need the unicode constants at all. Just use the character viewer and type the character directly. 😝

let sparklingHeart = "💖"

Comments

11

1 Decoding the Unicode:

extension String {
    var decodeEmoji: String{
        let data = self.data(using: String.Encoding.utf8);
        let decodedStr = NSString(data: data!, encoding: String.Encoding.nonLossyASCII.rawValue)
        if let str = decodedStr{
            return str as String
        }
        return self
    }
}

Usage

let decodedString = yourString.decodeEmoji

2 Encoding the Unicode:

extension String {
    var encodeEmoji: String{
        if let encodeStr = NSString(cString: self.cString(using: .nonLossyASCII)!, encoding: String.Encoding.utf8.rawValue){
            return encodeStr as String
        }
        return self
    }
}

Usage

let encodedString = yourString.encodeEmoji

Comments

8

You could insert the emoji directly using ⌘ ^ Space.

Or, based on Greg's answer:

var myText: String = "\u{e008}"

3 Comments

Problem is that he got Unicode values from a website that gives just rubbish values that don't work. E008 is just the wrong number.
Do you mean ctrl + ⌘ + Space?
Yea thats another way of saying it
5

As Greg posted above, you can directly input the emoji into Swift using the OSx character viewer. The character viewer is disabled by default. Here is how to enable it:

Go to System Preferences > Language and Region > Keyboard Preferences > Keyboard then check Show Keyboard, Emoji, & Symbol Viewers in menu bar. Once checked you can open the character viewer from the top right menu bar next to your Wifi and Date/Time icons.

Comments

2

This took me a bit of time to figure out in MacOS 11, so I thought I would share.

If you prefer to input the unicode characters rather than pasting literal emojis, you can find out the unicode for the system emojis like this:

  1. Focus/click into a text field (e.g. the search bar in your web browser).

  2. Press ctrl+cmd+space or go to Edit->Emoji & Symbols in the menu bar.

  3. Scroll up in the character viewer until you see the window expand icon in the upper right: Quick Character Viewer Expand

  4. In the expanded Character Viewer window, press the upper left button and select Customize List.... Character Viewer Customize List

  5. Scroll down to Code Tables minimized list, expand the list, toggle on Unicode, and press Done (system changed this window to dark mode for whatever reason). Code Tables

  6. Now, click the different emojis and you should see the unicode underneath the image. Unicode

  7. Then you inject it the unicode like this:

var myText: String = "\u{e008}"

Comments

1

from your Hex "0x1F52D" to actual Emoji

let c = 0x1F602

next step would possibly getting an Uint32 from your Hex

let intEmoji = UnicodeScalar(c!).value

from this you can do something like

titleLabel.text = String(UnicodeScalar(intEmoji)!)

here you have a "😂"

it work with range of hexadecimal too

let emojiRanges = [
            0x1F600...0x1F636,
            0x1F645...0x1F64F,
            0x1F910...0x1F91F,
            0x1F30D...0x1F52D
        ]

        for range in emojiRanges {
            for i in range {
                let c = UnicodeScalar(i)!.value
                data.append(c)
            }
        }

to get multiple UInt32 from your Hex range for exemple

1 Comment

what is the data type of "let c = 0x1F602" this variable? @Mehdi S.
0

Chris Slowik's and Greg's answers are close. The easiest answer is just to "rephrase" your String from this:

var myText: String = "\ue008"

To this:

var myText: String = "\u{008}"

The Unicodes found on the link you've attached are not wrong, as someone else claimed. You just need to rephrase it inside the String.

The important piece of code in your example above is the "008" part.

I've created a simple function to convert these kinds Unicode to their corresponding Emojis:

func convertHexToEmoji(_ u:Int) -> String {
     return "\(UnicodeScalar(u)!)" }

To use:

let myText = convertHexToEmoji(008)
print(myText)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.