Evan
Evan

Reputation: 1737

Using a unicode character in a UILabel through RubyMotion

I'm attempting to use a Unicode private space character and set it to the text property of a UILabel. This is using RubyMotion.

The character I want is part of the Entypo family and is U+1F554 (🕔).

I create a new UILabel:

@lblIcon = UILabel.alloc.initWithFrame([[0,(self.view.frame.size.height/2) - 128],[self.view.frame.size.width,96]])

And set it's text to the Unicode character using the pack syntax.

@lblIcon.text = [0x1f554].pack('U*')

I then apply the icon font and add it to the view:

ico_font = UIFont.fontWithName("Entypo", size:48)
@lblIcon.font = ico_font
self.view.addSubview @lblIcon

When I run rake and attempt to launch the app, I get the following crash message:

*** Terminating app due to uncaught exception 'RuntimeError', reason: 'ui_label.rb:16:in `font=:': NSInvalidArgumentException: NSConcreteMutableAttributedString addAttribute:value:range:: nil value (RuntimeError)

I've also tried

@lblIcon.text = [0x1f554].pack('U*') + ""

and

@lblIcon.text = "\U1F554"

to no avail.

What is the correct way to create a string composed of a unicode character suitable for use in a UILabel?

Upvotes: 1

Views: 380

Answers (2)

Devon
Devon

Reputation: 118

GantMan is right.

Setting the label's text to '0x1f554'.hex.chr(Encoding::UTF_8) should work.

Upvotes: 1

Gant Laborde
Gant Laborde

Reputation: 6774

Here's a full gem solution of someone doing this with FontAwesome

https://github.com/derailed/motion-awesome?source=c

Looks like he uses index.hex.chr(Encoding::UTF_8)

Upvotes: 0

Related Questions