如何设置作为属性字符串附件的模板化图像的颜色?
背景:
我有一个UILabel,我将其attributionText设置为NSAttributedString.NSAttributedString包含带有小图像的NSTextAttachment.现在我想让我的图像颜色与文本颜色匹配,我无法弄清楚如何使它工作.
我通常希望通过将其渲染模式设置为UIImageRenderingModeAlwaysTemplate然后在包含UIView上设置tintColor来为图像着色.我试过在我的UILabel上设置tintColor但是没有效果.
这是我的代码.它在Ruby(RubyMotion)中,因此语法可能看起来有点滑稽,但它与Objective C 1:1映射.
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
Run Code Online (Sandbox Code Playgroud)