【发布时间】:2012-08-21 22:10:12
【问题描述】:
我是 mac 开发的新手。我正在通过继承 NSTextView 来开发一个关于richEdit 的应用程序。在我自定义的 textView 中,我可以插入 .bmp .jpeg .png 之类的图像。代码是这样的:
NSTextAttachment* attachment;
NSImage *image = [[NSImage alloc] initWithContentsOfFile:strRealPath]; //strRealPath is the absolute path of image;
attachment = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *cell = [[NSTextAttachmentCell alloc] initImageCell:image];
[attachment setAttachmentCell:cell];
NSMutableAttributedString *attachmentString = (NSMutableAttributedString*)[NSMutableAttributedString attributedStringWithAttachment:attachment];
NSTextStorage* textStorage = [self textStorage];
[textStorage appendAttributedString:attachmentString];
[self didChangeText]; //self is my customized texview
通过这种方法,那些图像效果很好,但是当图像是gif时,动画不显示。所以,我试着这样显示:
NSTextAttachment* attachment;
NSImage *image = [[NSImage alloc] initWithContentsOfFile:strRealPath]; //strRealPath is the absolute path of image;
attachment = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *cell = [[NSTextAttachmentCell alloc] initImageCell:image];
NSMutableAttributedString *attachmentString = (NSMutableAttributedString*)[NSMutableAttributedString attributedStringWithAttachment:attachment];
NSTextStorage* textStorage = [self textStorage];
[textStorage appendAttributedString:attachmentString];
[self didChangeText]; //self is my customized texview
最后,gif 动画成功了。但是我的问题来了:当我单击或选择gif图像时,右键单击上下文菜单不显示,但其他类型图像的上下文菜单效果很好。
【问题讨论】:
-
你真的设法在 NSTextView 中运行一个动画 gif 吗?这个帖子表明它可能是不可能的:stackoverflow.com/questions/29949348/… 您在上面粘贴的第二个代码省略了在附件上设置一个单元格 - 这真的是修复吗?你能确认吗?谢谢。
标签: objective-c gif nstextview nsfilewrapper