I want to combine image and text inside UILabel
. For this, I use this part of the 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))
The text has a white foreground color, but unfortunately the image is still in its original color. Interestingly, when I change the first line to this line (space inside the initializer):
let attributedText = NSMutableAttributedString(string: " ")
then everything works fine. But the problem is that all the text inside the label is biased due to spaces. How to change image color without adding spaces?
source
share