我该如何添加令牌,比如 NSTokenField
,到 NStextView
?
我该如何添加令牌,比如 NSTokenField
,到 NStextView
?
这实际上有点复杂。您需要创建自定义 NSTextAttachment
为每个“令牌”并将其插入 NSTextStorage
为您 NSTextView
。
有一个 Dejal Systems的David Sinclair的精彩帖子 这解释了如何做到这一点。
这实际上有点复杂。您需要创建自定义 NSTextAttachment
为每个“令牌”并将其插入 NSTextStorage
为您 NSTextView
。
有一个 Dejal Systems的David Sinclair的精彩帖子 这解释了如何做到这一点。
我想出了一个简单的方法,它使用自定义单元格类来标记:
NSTextAttachmentCell
并重新实现- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
NSTextView
。NSTextAttachment
将标记插入文本视图的方法可能如下所示:
- (void)insertAttachmentCell:(NSTextAttachmentCell *)cell toTextView:(NSTextView *)textView
{
NSTextAttachment *attachment = [NSTextAttachment new];
[attachment setAttachmentCell:cell];
[textView insertText:[NSAttributedString attributedStringWithAttachment:attachment]];
}
这种方法比标记更适合于令牌 大卫辛克莱。因为我们想要显示动态内容(令牌)而不是静态图像,所以不需要使用文件包装器。
看看大卫的概念可能会有用。他描述了一种实现拖放操作的好方法。复制粘贴功能。