【问题标题】:Proper way to replace NSTextStorage in NSTextView?在 NSTextView 中替换 NSTextStorage 的正确方法?
【发布时间】:2013-04-12 02:39:57
【问题描述】:

我正在制作一些文本查看器应用程序。目前我需要非常频繁和精确的线路处理能力,所以我想继承NSTextStorage类。但我找不到将新文本存储设置为NSTextView 的任何方法。我能找到的唯一方法是

-[NSLayoutManager replaceTextStorage:]

方法。但令人困惑的是这是否是我想要的。因为它似乎只是替换了链接的 NSLayoutManagers 而不是 NSTextView 的文本存储。

我也考虑过继承 NSTextView 并覆盖 -textStorage 方法,但是如果该类不是为子类设计的,则会产生未定义的结果。

有人尝试在 NSTextView 上使用自定义 NSTextStorage 吗?我怎样才能做到这一点?或者这是设计禁止的?

【问题讨论】:

    标签: cocoa nstextview subclassing nstextstorage


    【解决方案1】:

    您可以执行以下操作来更改 NSTextView 的存储:

    NSTextStorage *newStorage = [[NSTextStorage alloc] initWithString: @"test"];
    
    [aTextView.layoutManager replaceTextStorage: newStorage];
    

    由于 NSTextStorage 是 NSMutableAttributedString 的子类,您可以使用所有相同的方法来操作它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多