Create IBInspectable for Unicode Character

I created some icons using websites like ( http://fontastic.me/ ) which gives you a .ttf file.

you can use it

myLable.text = "\u{e002}" //๎€‚
print("\u{e002}")     //๎€‚
print("\u{1F496}")   //๐Ÿ’–

this works well, but I want to pass the line directly using the storyboard.

Then I start working with a subclass of UILabel and create @IBInsectable for the unicode string. but it didnโ€™t work. following IBInspectable code.

@IBInspectable internal var myString: String? {
        didSet {
            text = "\u{e002}"       //๎€‚          this works
            text = "\\u{\(myString)}" //\u{e002}    this not
        }

or

let myString = "e002"
print("\\u{\(myString)}")   //\u{e002}

even this also doesn't work

print(myString)
text = String(UTF8String: myString!)

but it will only print text that would suggest printing an icon, for example print("\u{e002}"). How to decide what I am missing?

in advance for help.

+4
source share
1

NSScanner, NSString unicode

IBInspectable \u, NSScanner NSString Unicode, Unicode

\ue002, e002 NSSString

, , , UILabel IBInspectable.

class LabelSubClass: UILabel {

    @IBInspectable internal var myString: String? {
        didSet {
            let scanner: NSScanner = NSScanner(string: myString!)
            var code: UInt32 = 0
            scanner.scanHexInt(&code)
            let unicodeString: String = String(format:"%C", UInt16(code))
            print("unicodeString: \(unicodeString)")

            text = unicodeString
        }
    }
}

. , unicode

+6

All Articles