23

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 ?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Cherif
  • 5,223
  • 8
  • 33
  • 54

9 Answers9

50

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"
John Doe
  • 2,173
  • 1
  • 21
  • 12
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
Greg
  • 25,317
  • 6
  • 53
  • 62
  • can you please share me that swift documentation link @Greg – R. Mohan May 21 '18 at 09:25
  • @R.Mohan please see tis link https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html – Greg May 21 '18 at 10:37
11

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

let sparklingHeart = ""
gnasher729
  • 51,477
  • 5
  • 75
  • 98
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
Lijith Vipin
  • 1,870
  • 21
  • 29
7

You could insert the emoji directly using ⌘ ^ Space.

Or, based on Greg's answer:

var myText: String = "\u{e008}"
Chris Slowik
  • 2,859
  • 1
  • 14
  • 27
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.

Peter Coyle
  • 61
  • 1
  • 3
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

Mehdi S.
  • 471
  • 1
  • 4
  • 18
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)
Dharman
  • 30,962
  • 25
  • 85
  • 135
mjoe7
  • 134
  • 6
0

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}"
Mykel
  • 1,355
  • 15
  • 25