Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created June 10, 2020 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinGutowski/194e3bdbead59d12f4da399e994dca3a to your computer and use it in GitHub Desktop.
Save KevinGutowski/194e3bdbead59d12f4da399e994dca3a to your computer and use it in GitHub Desktop.
Image Attachment to a Text Object
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selection = doc.selectedLayers.layers[0]
let imgPath = "/Users/Kski/Downloads/image-2.png"
let fileWrapper = NSFileWrapper.alloc().initWithPath(imgPath)
let attachment = NSTextAttachment.alloc().initWithFileWrapper(fileWrapper)
attachment.bounds = NSMakeRect(0,0,100,30) //seems like these bounds don't do anything?
// Currently could only figure out how to add it in when editing
let textStorage = selection.sketchObject.editingDelegate().textView().textStorage()
let attachmentAttr = NSAttributedString.attributedStringWithAttachment(attachment)
textStorage.replaceCharactersInRange_withAttributedString(NSMakeRange(4,0),attachmentAttr)
// See example code here: https://github.com/plumhead/CocoaTextAttachmentCells
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment