Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView insert image in between text

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.

like image 306
Amitg2k12 Avatar asked Mar 14 '11 15:03

Amitg2k12


1 Answers

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;
like image 160
Anne Avatar answered Oct 23 '22 13:10

Anne