问题
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
?
回答1:
GantMan is right.
Setting the label's text to '0x1f554'.hex.chr(Encoding::UTF_8)
should work.
回答2:
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)
来源:https://stackoverflow.com/questions/16798901/using-a-unicode-character-in-a-uilabel-through-rubymotion