Nominalista
Nominalista

Reputation: 4838

Foreground color not working for attributed text

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?

Upvotes: 5

Views: 2140

Answers (3)

Maria
Maria

Reputation: 134

Turns out the bug in UIKit still persists, but to avoid the offset due to the space, you can use an empty string instead of the space. Just do:

let attributedText = NSMutableAttributedString(string: "\0")

instead of

let attributedText = NSMutableAttributedString(string: " ")

Upvotes: 0

Arun Balakrishnan
Arun Balakrishnan

Reputation: 1502

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)
    }
}

Upvotes: 4

Alex Wally
Alex Wally

Reputation: 2349

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)

Upvotes: 1

Related Questions