【发布时间】:2009-09-21 20:07:10
【问题描述】:
如何将NSTokenField 等令牌添加到NStextView?
【问题讨论】:
标签: objective-c cocoa nstextview
如何将NSTokenField 等令牌添加到NStextView?
【问题讨论】:
标签: objective-c cocoa nstextview
这实际上有点复杂。您需要为每个“令牌”创建一个自定义 NSTextAttachment,并将其插入到您的 NSTextView 的 NSTextStorage 中。
有一个great post by David Sinclair at Dejal Systems 解释了如何操作。
【讨论】:
我想出了一种使用自定义单元类作为标记的简单方法:
NSTextAttachmentCell 的单元类并重新实现- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlViewNSTextView 中标记的类。NSTextAttachment 的实例
将标记插入文本视图的方法可能如下所示:
- (void)insertAttachmentCell:(NSTextAttachmentCell *)cell toTextView:(NSTextView *)textView
{
NSTextAttachment *attachment = [NSTextAttachment new];
[attachment setAttachmentCell:cell];
[textView insertText:[NSAttributedString attributedStringWithAttachment:attachment]];
}
这种方法比David Sinclair 的方法更适合令牌。无需使用文件包装器,因为我们希望显示动态内容(令牌)而不是静态图像。
不过,看看 David 的概念可能会很有用。他描述了实现拖放响应的好方法。复制粘贴功能。
【讨论】: