4

I've created some Icon Fonts using websites like (http://fontastic.me/) which gives you .ttf file.

you can use it by

myLable.text = "\u{e002}" //
print("\u{e002}")     //
print("\u{1F496}")   //

this works nicely but I want to pass string directly using storyboard.

Then I start working with UILabel Subclass and create @IBInsectable for unicode string. but this was not working. following is code of IBInspectable.

@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 not working

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

but this will print only text it's suppose to print Icon like print("\u{e002}") this. how to solve this, what I'm missing ?

thanks in advance for any help.

1 Answers1

6

You need to use NSScanner class that helps you to convert NSString to unicode

On IBInspectable just pass only code without \u use NSScanner class to convert NSString to Unicode and you can use as unicode character

Like if you want to use \ue002 then only set e002 to NSSString

Below is example what you want, like subclass of UILabel and have property with IBInspectable element.

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
        }
    }
}

See here how to set unicode to inspectable element
Here you can see output of above code

Paresh Patel
  • 380
  • 4
  • 14