How to pull NSImage from NSTextAttachment in NSTextView?


Goal is to allow user to add NSImage(s) to an NSAttributedString in an NSTextView, and then reverse the process and extract the image(s). With code from <a href="https://stackoverflow.com/questions/36105805/nstextattachment-image-not-shown-in-nstextview-but-in-uitextview" rel="nofollow">here</a>, can add image(s) and have them displayed inline.

let attributedText = NSMutableAttributedString(string: string, attributes: attributes as? [String : AnyObject]) let attachment = NSTextAttachment() let imageTest = NSImage(named:"sampleImage") as NSImage? let attachmentCell: NSTextAttachmentCell = NSTextAttachmentCell.init(imageCell: imageTest) attachment.attachmentCell = attachmentCell let imageString = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment)) attributedText.append(imageString) textView.textStorage?.setAttributedString(attributedText)

Can deconstruct this as far as a legit (non-nil) NSTextAttachment, but uncertain how to extract the image.

if (textView.textStorage?.containsAttachments ?? false) { let runs = textView.textStorage?.attributeRuns if runs != nil { for run in runs! { var r = NSRange() let att = run.attributes(at: 0, effectiveRange: &r) let z = att[NSAttachmentAttributeName] as? NSTextAttachment if z != nil { Swift.print(z!) // z!.image and z!.contents are both nil } } } }

I appreciate any guidance on how to pull the image(s) from this. Thank you.


Since the NSImage was created with an NSTextAttachmentCell, it must be retrieved from the attachment cell. The key is to cast the cell as NSCell and then grab the image property. So, replacing the section of code from the original

if z != nil { let cell = z!.attachmentCell as? NSCell if cell != nil { let image = cell?.image } }


  • how to display json data in my listview in intel xdk
  • How do I add a show/hide to Questionnaire form
  • Global variables modified by main() and accessed by ISR()
  • Reading the last line in a Tmemo in delphi
  • reloading my drop down when using a custom directive
  • Using regex to fix xml content
  • not retrieving html value through xslt
  • Read XML in spark
  • Style.display block/none problems
  • Split into 3 character length
  • How to force culture/region on RegionInfo.DisplayName
  • How to re-write timthumb.php image url?
  • How to extract C source code from .so file?
  • Problem getting terminal output from ImageMagick's compare.exe ( Either by pipe or Python )
  • Macro to cycle through and allocate data to members of structs incorrectly recognises struct member
  • How to know the width of last line of label?
  • How to update a table based on an XML parameter
  • Search String in NSDictionary store in NSMutableArray
  • Odoo POS create a dropdown list
  • Why is my segue causing an “unrecognized selector sent to instance” NSInvalidArgumentException?
  • Swift Cast Generics Type
  • option[selected=true] doesn't work
  • Google App Indexing not resolving for Swift?
  • python object not iterable error in function
  • iOS 9 errors and correct conversion to swift 2
  • Thread synchronization with syncwarp
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • How to access profile picture with Facebook API in Swift 3?
  • Django simple Captcha “No module named fields” error
  • Why does access(2) check for real and not effective UID?
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++