代码之家  ›  专栏  ›  技术社区  ›  gcstr

带标记的nstextview

  •  12
  • gcstr  · 技术社区  · 15 年前

    如何添加令牌,例如 NSTokenField ,到 NStextView ?

    2 回复  |  直到 12 年前
        1
  •  9
  •   DD_ NiravPatel    12 年前

    这其实有点复杂。您需要创建自定义 NSTextAttachment 对于每个“令牌”,并将其插入 NSTextStorage 为了你 NSTextView .

    有一个 great post by David Sinclair at Dejal Systems 这就解释了怎么做。

        2
  •  4
  •   DD_ NiravPatel    12 年前

    我发现了一种简单的方法,它使用一个定制的单元类作为令牌:

    1. 编写继承的单元类 NSTextAttachmentCell 再实施
      - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
      这将是表示您的 NSTextView .
    2. 要插入令牌,请执行以下步骤:
      1. 创建的实例 NSTextAttachment
      2. 将附件的单元格设置为标记单元格类的实例。
      3. 使用该附件创建属性化字符串。
      4. 将属性化字符串插入文本视图。

    将标记插入文本视图的方法可能如下所示:

    - (void)insertAttachmentCell:(NSTextAttachmentCell *)cell toTextView:(NSTextView *)textView
    {
        NSTextAttachment *attachment = [NSTextAttachment new];
        [attachment setAttachmentCell:cell];
        [textView insertText:[NSAttributedString attributedStringWithAttachment:attachment]];
    }
    

    这种方法比 David Sinclair .不需要使用文件包装器,因为我们希望显示动态内容(令牌)而不是静态图像。
    不过,看看大卫的概念可能是有用的。他描述了实现拖放响应的良好方法。复制粘贴功能。