NSTextView在文本之间插入图像

时间:2011-03-14 15:04:55

标签: objective-c cocoa macos nstextview nsimage

是否可以将图像(不是背景图像)插入NSTextView? 类似的东西:

Hi :) How are you?

它应该显示“笑脸”图像。我有NSTextViewNSImage

1 个答案:

答案 0 :(得分:36)

将NSImage插入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];

在头文件中:

IBOutlet id textView;