objective-c - NSTextView 在文本之间插入图像

标签 objective-c cocoa macos nstextview nsimage

是否可以将图像(不是背景图像)插入到 NSTextView 中? 像这样的东西:

Hi :) How are you?

它应该显示一个“笑脸”图像。我有一个 NSTextView 和一个 NSImage

最佳答案

将 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;

关于objective-c - NSTextView 在文本之间插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300427/

相关文章:

ios - 如何检查三个按钮条件

objective-c - 如何在 Objective-c 中编码非 C 数据类型?

cocoa : Load NSViewController with nib

Excel for mac 隐藏功能区

ios - 多个按钮的设计模式

ios - NSURLSessionDownloadTask 从其他 View ios 恢复和暂停

ios - 尝试节省 UIWebView 内 html 的加载时间

objective-c - Cocoa 基于文档的应用程序中带有窗口文档图标弹出菜单的错误

objective-c - OSX/Xcode/Objective-C : How to detect the version of Flash Player installed?

macos - Mac OS X 盖子打开/关闭事件日志