Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include emoticons in Swift string?

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 ?

like image 497
Cherif Avatar asked Sep 10 '15 15:09

Cherif


People also ask

How do I show Emojis in Swift?

The emoji menu is accessed from the keyboard by tapping or long pressing the emoji/enter key in the bottom right corner, or via the dedicated emoji key in the bottom left (depending on your settings). With the 'Dedicated emoji key' checked, just tap on the emoji (smiley) face to open the emoji panel.

Are Emojis included in Unicode?

Because emoji characters are treated as pictographs, they are encoded in Unicode based primarily on their general appearance, not on an intended semantic. The meaning of each emoji can vary depending on language, culture, context, and may change or be repurposed by various groups over time.


4 Answers

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"
like image 113
John Doe Avatar answered Oct 09 '22 20:10

John Doe


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
like image 21
Greg Avatar answered Oct 09 '22 18:10

Greg


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

let sparklingHeart = "💖"
like image 11
gnasher729 Avatar answered Oct 09 '22 19:10

gnasher729


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
like image 11
Lijith Vipin Avatar answered Oct 09 '22 19:10

Lijith Vipin