Using a unicode character in a UILabel through RubyMotion

回眸只為那壹抹淺笑 提交于 2019-12-24 15:25:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!