13

Is it possible to insert an image (not a background image) into an NSTextView? Something like:

Hi :) How are you?

and it should display a "smiley" image. I have an NSTextView and an NSImage.

jscs
  • 63,694
  • 13
  • 151
  • 195
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97

1 Answers1

37

Insert NSImage into NSTextView:

NSImage * pic = [[NSImage alloc] initWithContentsOfFile:@"/Users/Anne/Desktop/Sample.png"];
NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
NSAttributedString *attributedString = [NSAttributedString  attributedStringWithAttachment: attachment];
[[textView textStorage] appendAttributedString:attributedString];

In the header file:

IBOutlet id textView;
Grimxn
  • 22,115
  • 10
  • 72
  • 85
Anne
  • 26,765
  • 9
  • 65
  • 71
  • thanks a lot, one more question is there any way to add anymore control like, if i want to add a progress bar when user wants to transfer a file – Amitg2k12 Mar 15 '11 at 05:11
  • Thanks! Do you know how to provide drag & drop with the image of the cell? By default if I drag it to Finder it creates an empty text file – hyouuu Nov 09 '18 at 02:19