我试图创建一个标签的文字,加上一个图标,但不幸的是,我不能改变只是图标的颜色。它总是用它的默认颜色着色。
我就是这样做的:
var titleStringAttribute = NSMutableAttributedString(string: "")
var iconAttachment = NSTextAttachment(image: UIImage(systemName: "trash")!)
iconAttachment.image?.withTintColor(.red, renderingMode: .alwaysTemplate) // Does not work?!
titleStringAttribute.append(NSAttributedString(attachment: iconAttachment))
// Appending bold text to attributedText
theLabel.attributedText = titleStringAttribute我还查看了StackOverflow和互联网上的其他网站,但没有任何帮助。
我正在使用iOS 13.5。
谢谢你的回答!
发布于 2020-05-14 00:32:52
withTintColor(_:renderingMode:)不会改变你要调用它的图像。它所做的都是返回一个新的UIImage供您使用,但您没有使用它。
更新您的代码,首先创建默认的垃圾映像,然后使用withTintColor创建一个新图像,然后在其初始化器中给附件添加新图像:
let titleStringAttribute = NSMutableAttributedString(string: "")
let trashImage = UIImage(systemName: "trash")!
let redTrashImage = trashImage.withTintColor(.red, renderingMode: .alwaysTemplate)
let iconAttachment = NSTextAttachment(image: redTrashImage)
titleStringAttribute.append(NSAttributedString(attachment: iconAttachment))
// Appending bold text to attributedText
theLabel.attributedText = titleStringAttributehttps://stackoverflow.com/questions/61785713
复制相似问题