私は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画像をクリックまたは選択しても、右クリックのコンテキストメニューが表示されませんが、他の種類の画像のコンテキストメニューは適切に機能します。