How can I set the color of a templated image that is an attachment on an attributed string?
Background:
I've got a UILabel and I'm setting its attributedText to an NSAttributedString. The NSAttributedString includes an NSTextAttachment with a small image. Now I want to make my image color match the text color and I can't figure out how to make it work.
I would normally expect to color the image by setting its rendering mode to UIImageRenderingModeAlwaysTemplate and then setting the tintColor on the containing UIView. I've tried setting the tintColor on my UILabel but that has no effect.
Here's my code. It's in Ruby (RubyMotion) so the syntax might look a little funny, but it maps 1:1 with 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
It seems that there's a bug in UIKit. There's a workaround for that ;]
For some reason you need to append empty space before image attachment to make it work properly with UIImageRenderingModeAlwaysTemplate
.
So your snippet would look like that (mine is in ObjC):
- (NSAttributedString *)attributedStringWithValue:(NSString *)string image:(UIImage *)image { NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = image; NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; [mutableAttributedString appendAttributedString:attachmentString]; [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:0] range:NSMakeRange(0, mutableAttributedString.length)]; // Put font size 0 to prevent offset [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, mutableAttributedString.length)]; [mutableAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]]; NSAttributedString *ratingText = [[NSAttributedString alloc] initWithString:string]; [mutableAttributedString appendAttributedString:ratingText]; return mutableAttributedString; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With