Skip to content

Instantly share code, notes, and snippets.

@danielbuechele
Created March 21, 2015 12:46
Show Gist options
  • Save danielbuechele/df0d17b6840e1513bfbc to your computer and use it in GitHub Desktop.
Save danielbuechele/df0d17b6840e1513bfbc to your computer and use it in GitHub Desktop.
Converting Emoji String to HTML and back
// 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