Created
March 21, 2015 12:46
-
-
Save danielbuechele/df0d17b6840e1513bfbc to your computer and use it in GitHub Desktop.
Converting Emoji String to HTML and back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
let str = "Test 🇯🇵" | |
var str2 = "" | |
for char in str.unicodeScalars { | |
if (char.value>7936) { | |
str2 += "&#"+String(Int(char.value))+";" | |
} else { | |
str2 += String(char) | |
} | |
} | |
str2 | |
let encodedData = str2.dataUsingEncoding(NSUTF8StringEncoding)! | |
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] | |
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil) | |
let decodedString = attributedString!.string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment