我有一个sklabelnode,它的字符串我想用图标替换。
let attachment = NSAttributedString(attachment: txt)
attributedString.replaceCharacters(in: NSRange(location: range.location, length: range.length), with: attachment)
但是,当实际输出时,那里什么都没有!字符已成功删除,但没有插入文本附件。当我尝试子类NSTEXTATTACHMENT时,attachmentBounds
也未调用。
我的问题:使用SpriteKit时,将其工作或获得在文本块中的更好方法是否有一些技巧?
只是遇到了同一问题,并且在堆栈溢出上进行了一些搜索,决定使用SKSpriteNode Child Solution。
if let attributedText = _labelnode.attributedText {
attributedText.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedText.length), options: []) { (value, range, stop) in
if value is NSTextAttachment {
if let attachment = value as? NSTextAttachment,
let image = attachment.image {
if let tintedImage = image.tinted(with: legendEntry.colour) {
let texture = SKTexture(image: tintedImage)
let imageSpriteNode = SKSpriteNode(texture: texture)
_labelnode.addChild(imageSpriteNode)
}
}
}
}
}
需要相应地定位和收缩图像。