通过RubyMotion在UILabel中使用unicode字符
我试图使用Unicode私有空间字符并将其设置为UILabel
的text
属性。 这是使用RubyMotion。
我想要的angular色是Entypo家族的一部分,是U+1F554
( 🕔
)。
我创build一个新的UILabel
:
@lblIcon = UILabel.alloc.initWithFrame([[0,(self.view.frame.size.height/2) - 128],[self.view.frame.size.width,96]])
并使用pack
语法将其文本设置为Unicode字符。
@lblIcon.text = [0x1f554].pack('U*')
然后,我应用图标字体并将其添加到视图中:
ico_font = UIFont.fontWithName("Entypo", size:48) @lblIcon.font = ico_font self.view.addSubview @lblIcon
当我运行rake
并尝试启动应用程序时,出现以下消息:
*** Terminating app due to uncaught exception 'RuntimeError', reason: 'ui_label.rb:16:in `font=:': NSInvalidArgumentException: NSConcreteMutableAttributedString addAttribute:value:range:: nil value (RuntimeError)
我也试过了
@lblIcon.text = [0x1f554].pack('U*') + ""
和
@lblIcon.text = "\U1F554"
无济于事。
什么是正确的方法来创build一个string组成的适合在UILabel
使用的Unicode字符?
甘特曼是对的。
将标签的文本设置为'0x1f554'.hex.chr(Encoding::UTF_8)
应该可以工作。
这是一个用FontAwesome做这个的完整的gem解决scheme
https://github.com/derailed/motion-awesome?source=c
看起来他使用index.hex.chr(Encoding::UTF_8)