在NSTextView中替换NSTextStorage的正确方法

在NSTextView中替换NSTextStorage的正确方法

本文介绍了在NSTextView中替换NSTextStorage的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一些文本查看器应用程序。当前,我需要非常频繁且精确的行处理能力,因此我想将 NSTextStorage 类作为子类。但是我找不到任何方法来将新的文本存储设置为 NSTextView 。我能找到的唯一方法是

I am making some text viewer app. Currently I need very frequent and precise line handling ability, so I want to subclass NSTextStorage class. But I couldn't find any method to set a new text storage to NSTextView. The only method I could find was

-[NSLayoutManager replaceTextStorage:]

方法。但这是否就是我想要的。因为似乎只是替换链接的NSLayoutManager的文本存储而不是NSTextView。

method. But it's confusing whether this is what I was looking for. Because it seems just replace text storage of linked NSLayoutManagers instead of NSTextView.

我还考虑了NSTextView的子类化并覆盖了 -textStorage 方法,但是如果该类不是为子类设计的,它将产生不确定的结果。

I also considered subclassing NSTextView and overriding -textStorage method, but if the class is not designed for subclassing, it will make undefined result.

有人尝试过在NST​​extView上使用自定义NSTextStorage吗?我怎样才能做到这一点?还是这是设计所禁止的?

Anyone has tried to use custom NSTextStorage on NSTextView? How can I do this? Or is this prohibited by design?

推荐答案

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

You can do something like this to change the storage for an NSTextView:

NSTextStorage *newStorage = [[NSTextStorage alloc] initWithString: @"test"];

[aTextView.layoutManager replaceTextStorage: newStorage];

由于NSTextStorage是NSMutableAttributedString的子类,因此可以使用所有相同的方法对其进行操作。

Since NSTextStorage is a subclass of NSMutableAttributedString you can manipulate it with all of the same methods.

这篇关于在NSTextView中替换NSTextStorage的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:51