【问题标题】:NSTextView with tokens带有令牌的 NSTextView
【发布时间】:2009-09-21 20:07:10
【问题描述】:

如何将NSTokenField 等令牌添加到NStextView

【问题讨论】:

    标签: objective-c cocoa nstextview


    【解决方案1】:

    这实际上有点复杂。您需要为每个“令牌”创建一个自定义 NSTextAttachment,并将其插入到您的 NSTextViewNSTextStorage 中。

    有一个great post by David Sinclair at Dejal Systems 解释了如何操作。

    【讨论】:

    • 谢谢伙计。我不知道从哪里开始寻找它。这是一个伟大的开始。
    • 哇,这篇文章早在 2007 年就写好了……它适用于 TextView 的较新 iOS 7 TextKit 实现吗?
    • 这篇文章是为 MacOS 写的。 iOS 上的最新功能是什么?
    【解决方案2】:

    我想出了一种使用自定义单元类作为标记的简单方法:

    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 的方法更适合令牌。无需使用文件包装器,因为我们希望显示动态内容(令牌)而不是静态图像。
    不过,看看 David 的概念可能会很有用。他描述了实现拖放响应的好方法。复制粘贴功能。

    【讨论】:

    • 在没有 NSTextAttachmentCell 类的 iOS 上怎么样?
    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 2020-11-09
    • 2017-07-01
    • 2014-10-24
    • 2013-06-02
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多