ios - 在 RubyMotion 中子类化 UILabel 以设置 attributedText kerning

标签 ios objective-c uilabel rubymotion

我需要支持通过 Interface Builder (IB) 添加的一些标签的自定义字距调整。自定义类在 IB 中的标签上设置,文本也在那里设置。尝试使用 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

关于ios - 在 RubyMotion 中子类化 UILabel 以设置 attributedText kerning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20577664/

相关文章:

objective-c - 点击表格行时显示警报 View

ios - 为什么允许在实现内部的@interface中重写ivar,但是具有这种奇怪的行为?

iphone - ZipKit 和 Xcode 4

ios - 真的很接近 NSAttributedString 吗?

使用蓝牙控制 iOS AVPlayer

ios - 在 iOS Swift 中获取所有 UIViewControllers 列表

ios - 覆盖 UILabel 的布局以添加带有手势处理的 subview

iphone - UILabels 完整文本不可见

ios - 如何将 indexPath 传递给 CustomCell

iphone - 更新 block 中的 NSUInteger 属性。警告 : Block will be retained by