Wednesday, November 15, 2017

Foreground color not working for attributed text

Leave a Comment

I want to combine image and text inside UILabel. To accomplish that I'm using this part of code:

let attributedText = NSMutableAttributedString(string: "") let attachment = NSTextAttachment() attachment.image = image.withRenderingMode(.alwaysTemplate) attachment.bounds = CGRect(x: 0, y: 0, width: 20, height: 20) attributedText.append(NSAttributedString(attachment: attachment)) attributedText.append(NSMutableAttributedString(string: "test", attributedText.addAttribute(NSAttributedStringKey.foregroundColor,                 value: UIColor.white,                 range: NSMakeRange(0, attributedText.length)) 

Text has white foreground color, but unfortunately image is still in original color. Interestingly, when I change first line to this (whitespace inside initializer):

let attributedText = NSMutableAttributedString(string: " ") 

then everything is working fine. But the problem is that whole text inside label has offset due to whitespace. How can I change image color without adding whitespace?

2 Answers

Answers 1

You would have to use graphics context to invert colors. I would probably have this as an extension on UIImage.

Sample swift code is as follows.

    extension UIImage {     func imageWithColor(color:UIColor) -> UIImage {         let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height);         UIGraphicsBeginImageContext(rect.size)         let context = UIGraphicsGetCurrentContext()         context?.clip(to: rect, mask: self.cgImage!)         context?.setFillColor(color.cgColor)         context?.fill(rect)         let imageFromCurrentContext = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return UIImage(cgImage: imageFromCurrentContext!.cgImage!, scale: 1.0, orientation:.downMirrored)     } } 

Answers 2

That behavior seems to be a bug in UIKit. Alas I don't know of a solution, hopefully someone else does, but for now here's a workaround:

You can color the image before adding it as a text attachment. An easy way to do that is to use a third-party framework, e.g. this one: https://github.com/vilanovi/UIImage-Additions

Then instead of image.withRenderingMode(...) you can simply write:

attachment.image = image.add_tintedImage(with: .white, style: ADDImageTintStyleKeepingAlpha) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment