我是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 图像时,右键单击上下文菜单不显示,但其他类型图像的上下文菜单效果很好。