在RubyMotion中为UILabel子类以设置attributedText紧排



我需要支持通过接口生成器(IB)添加的一些标签的自定义紧排。自定义类设置在IB中的Label上,文本也设置在那里。尝试用attributedText属性重写text属性无效。

这对我的UIButton子类来说很好,但类似的技术在UILabel中不起作用。

当在awakeFromNibdrawRect: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

最新更新