Reputation: 831
I'm trying to add an image to an NSMutableAttributedString via NSTextAttachment, the trouble is that the image doesn't seem to appear until I restart the app. Any ideas?
var attributesForThisSection = [String : Any]()
if key == NSAttachmentAttributeName {
let base64Rep = (value as! String)
if let data = Data(base64Encoded: base64Rep) {
let textAttachment = NSTextAttachment()
if let img = NSImage(data: data) {
textAttachment.image = img
attributesForThisSection[NSAttachmentAttributeName] = textAttachment
}
else {
print("img from data not ok")
}
}
}
Then
let r = NSMakeRange(loc, len)
attributedString?.addAttributes(attributesForThisSection, range: r)
The text is displayed in an NSTextView, thanks!
Upvotes: 0
Views: 243
Reputation: 831
Turns out all I had to do was replace
textAttachment.image = img
With
let attachmentCell = NSTextAttachmentCell(imageCell: img)
textAttachment.attachmentCell = attachmentCell
Upvotes: 1