我需要支持通过 Interface Builder (IB) 添加的一些标签的自定义字距调整。自定义类在 IB 中的标签上设置,文本也在那里设置。尝试使用 attributedText
属性覆盖 text
属性不起作用。
这对于我的 UIButton
子类工作正常,但类似的技术不适用于 UILabel
。
在 awakeFromNib
或 drawRect:rect
中设置时,attributedText
属性似乎没有任何影响。
例子
class KernedLabel < UILabel
def awakeFromNib
super
attributed_text = NSMutableAttributedString.alloc
.initWithString("Atributed Text")
attributed_text.addAttribute(NSKernAttributeName,
value: 1.0,
range: [0, attributed_text.length])
attributedText = attributed_text
end
end
最佳答案
你的代码对我来说只需要一个小改动就可以正常工作:你需要使用 self.attributedText =
(调用 self
上的方法):
def awakeFromNib
super
attributed_text = NSMutableAttributedString.alloc
.initWithString("Atributed Text")
attributed_text.addAttribute(NSKernAttributeName,
value: 1.0,
range: [0, attributed_text.length])
self.attributedText = attributed_text
end
关于ios - 在 RubyMotion 中子类化 UILabel 以设置 attributedText kerning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20577664/