如何在NSTextAttachment中设置模板图像的颜色

如何设置作为属性string附件的模板化图像的颜色?

背景:

我有一个UILabel,我将其设置为一个NSAttributedString。 NSAttributedString包含一个带有小图片的NSTextAttachment。 现在我想让我的图像颜色匹配文本颜色,我无法弄清楚如何使其工作。

我通常希望通过将其渲染模式设置为UIImageRenderingModeAlwaysTemplate,然后在包含UIView上设置tintColor来着色图像。 我试过在我的UILabel上设置tintColor,但是没有效果。

这是我的代码。 它在Ruby(RubyMotion)中,所以语法可能看起来有点有趣,但是它与ObjectiveC 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 

看来UIKit有一个bug。 这是一个解决方法;]

出于某种原因,您需要在图像附件之前添加空白区域,以使其与UIImageRenderingModeAlwaysTemplate正常工作。

所以你的片段看起来像(我的是在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; } 

在使用UIImage+Additions时候,我有很好的使用UIImage+Additions的经验。 你可以在这里find它: https : //github.com/vilanovi/UIImage-Additions 。 特别检查第四节。

如果添加一个第三方库不是一个选项,这里是让你开始的东西:

 - (UIImage *)colorImage:(UIImage *)image color:(UIColor *)color { UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, image.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, rect, image.CGImage); CGContextSetBlendMode(context, kCGBlendModeSourceIn); [color setFill]; CGContextFillRect(context, rect); UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return coloredImage; } 

这将使UIImage从: 调色前的UIImage

UIImage着色后

@blazejmar解决scheme的工作,但没有必要。 所有你需要做的工作是在属性string连接后设置颜色。 这是一个例子。

 NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [[UIImage imageNamed:@"ImageName"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; NSString *string = @"Some text "; NSRange range2 = NSMakeRange(string.length - 1, attachmentString.length); NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString appendAttributedString:attachmentString]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range2]; self.label.attributedText = attributedString; 

对原始图像颜色使用UIImageRenderingModeAlwaysOriginal。 UIImageRenderingModeAlwaysTemplate自定义颜色。