NSTextView insert image in between text

前端 未结 1 826
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 11:52

Is it possible to insert an image (not a background image) into an NSTextView? Something like:

Hi :) How are you?

and it should display a

相关标签:
1条回答
  • 2020-12-14 11:59

    Insert NSImage into 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];
    

    In the header file:

    IBOutlet id textView;
    
    0 讨论(0)
提交回复
热议问题