3

当我通过 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];
4

2 回答 2

4

在此处输入图像描述

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

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

于 2015-02-11T09:01:59.080 回答
1

billibala 的上述回答也可以用代码实现。对于NSTextField

allowsEditingTextAttributes = true
于 2019-08-23T16:27:40.893 回答