首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextAttachment淡色

NSTextAttachment淡色
EN

Stack Overflow用户
提问于 2020-05-13 22:05:42
回答 1查看 847关注 0票数 3

我试图创建一个标签的文字,加上一个图标,但不幸的是,我不能改变只是图标的颜色。它总是用它的默认颜色着色。

我就是这样做的:

代码语言:javascript
复制
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。

谢谢你的回答!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-14 00:32:52

withTintColor(_:renderingMode:)不会改变你要调用它的图像。它所做的都是返回一个新的UIImage供您使用,但您没有使用它。

更新您的代码,首先创建默认的垃圾映像,然后使用withTintColor创建一个新图像,然后在其初始化器中给附件添加新图像:

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61785713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档