如何设置属性字符串中作为附件的模板图像的颜色?
背景:
我有一个UILabel并将其attributedText设置为NSAttributedString。NSAttributedString包括一个带有小图像的NSTextAttachment。现在我想让我的图像颜色与文本颜色匹配,但我无法弄清楚如何使其工作。
通常我会通过将渲染模式设置为UIImageRenderingModeAlwaysTemplate并设置包含UIView的tintColor来着色图像。我尝试在我的UILabel上设置tintColor,但没有效果。
这是我的代码。它是用Ruby(RubyMotion)编写的,因此语法可能看起来有些奇怪,但它可以1:1地映射到Objective C。
attachment = NSTextAttachment.alloc.initWithData(nil, ofType: nil)
attachment.image = UIImage.imageNamed(icon_name).imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate)
label_string = NSMutableAttributedString.attributedStringWithAttachment(attachment)
label_string.appendAttributedString(NSMutableAttributedString.alloc.initWithString('my text', attributes: { NSFontAttributeName => UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote), NSForegroundColorAttributeName => foreground_color }))
label = UILabel.alloc.initWithFrame(CGRectZero)
label.tintColor = foreground_color
label.attributedText = label_string
label.textAlignment = NSTextAlignmentCenter
label.numberOfLines = 0