Objective c NSTextField与带有附件的NSAttributedString的奇怪行为

Objective c NSTextField与带有附件的NSAttributedString的奇怪行为,objective-c,macos,cocoa,nsattributedstring,Objective C,Macos,Cocoa,Nsattributedstring,当我通过NSAttributedString在NSTextField中放置图像附件时,当我单击或更改它时,图像将消失。怎么了 NSTextAttachment* attachment = [[NSTextAttachment alloc] init]; NSImage *symbolImage = [NSImage imageNamed:@"enabled.png"]; NSLog(@"%@", symbolImage); NSTextAttachmentCell *anAttachmentCe

当我通过NSAttributedString在NSTextField中放置图像附件时,当我单击或更改它时,图像将消失。怎么了

NSTextAttachment* attachment = [[NSTextAttachment alloc] init];
NSImage *symbolImage = [NSImage imageNamed:@"enabled.png"];
NSLog(@"%@", symbolImage);
NSTextAttachmentCell *anAttachmentCell = [[NSTextAttachmentCell
                                           alloc] initImageCell:symbolImage];
[attachment setAttachmentCell:anAttachmentCell];
return [NSAttributedString attributedStringWithAttachment:attachment];

实际上,解决方法非常简单。只需在需要显示图像的字段中启用“富文本”


插入文本附件后,即使将焦点更改为另一个视图,图像仍会保留在那里。

上述由billibala提供的答案也可以用代码实现。 对于
NSTextField
set

allowsEditingTextAttributes = true

您能在代码中显示如何通过NSAttributedString进行图像附件吗?您解决了这个问题吗?我对链接也有同样的问题:不是,我没有:(请告诉我你是否愿意了解这个问题?我也有同样的问题。