Anthony Landreth
Anthony Landreth

Reputation: 35

Inserting an image attachment into an NSMutableAttributedString on macOS with Swift 4

I'd like to insert an image attachment into an NSMutableAttributedString on macOS. I've seen how this can work on iOS, but I'm not having much luck on macOS. On iOS, it can look like this:

let string = NSMutableAttributedString(string: "Some great text")
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "pic.png")
let imageString = NSAttributedString(attachment: attachment)
string.append(imageString)

On macOS, I'm trying this:

let string = NSMutableAttributedString(string: "yeah yeah yeah")
let attachment = NSTextAttachment()
attachment.image = NSImage(contentsOfFile: "<absolutePathTo>/pic.png")
let imageString = NSAttributedString(attachment: attachment)
string.append(imageString)

I expect the result to look like this:

NSAttachment = "<NSTextAttachment: 0x6000000e3b00> \"pic.png\""

But, the result is just an NSTextAttachment without a path:

NSAttachment = "<NSTextAttachment: 0x6040000e4700>"

FileManager says my png exists, given my path.

Upvotes: 0

Views: 540

Answers (0)

Related Questions