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

dar*_*vin 4 macos cocoa objective-c nsattributedstring

当我通过 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];
Run Code Online (Sandbox Code Playgroud)

bil*_*ala 5

在此输入图像描述

事实上,修复方法非常简单。只需在需要显示图像的字段中启用“富文本”即可。

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