gpt4 book ai didi

swift - NSTextAttachment 色调颜色

转载 作者:行者123 更新时间:2023-12-05 02:54:30 25 4
gpt4 key购买 nike

我正在尝试创建一个带有文本的标签,并在前面加上一个图标,但不幸的是我无法仅更改图标的颜色。它始终使用默认颜色着色。

我是这样做的:

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。

感谢您的回答!

最佳答案

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 = titleStringAttribute

关于swift - NSTextAttachment 色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61785713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com