NSTextView insert image in between text

﹥>﹥吖頭↗ 提交于 2019-11-27 13:23:44

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/5300427/nstextview-insert-image-in-between-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!