我需要支持通过接口生成器(IB)添加的一些标签的自定义紧排。自定义类设置在IB中的Label上,文本也设置在那里。尝试用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