我正在尝试使用Unicode private 空间字符并将其设置为text
的UILabel
属性。这是使用RubyMotion。
我想要的字符是Entypo家族的一部分,并且是U+1F554
(🕔
)。
我创建一个新的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"
无济于事。
创建由适合
UILabel
的unicode字符组成的字符串的正确方法是什么? 最佳答案
GantMan是正确的。
将标签的文本设置为'0x1f554'.hex.chr(Encoding::UTF_8)
应该可以。