正确的方式NSTextView更换NSTextStorage?(Proper way to repl

2019-08-17 19:08发布

我想提出一些文本查看器应用程序。 目前,我需要非常频繁且精确的线条处理能力,所以我想继承NSTextStorage类。 但是,我找不到任何方法来设置一个新的文本存储到NSTextView 。 我能找到的唯一的方法是

-[NSLayoutManager replaceTextStorage:]

方法。 但它的混乱,这是否就是我一直在寻找。 因为它似乎只是更换联系NSLayoutManagers代替NSTextView的文本存储。

我也算是继承NSTextView和压倒一切的-textStorage方法,但如果类不适合子类,它将使未定义的结果。

任何人试图在NSTextView使用自定义NSTextStorage? 我怎样才能做到这一点? 或者是这个设计被禁止?

Answer 1:

你可以做这样的事情来改变一个NSTextView存储:

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

[aTextView.layoutManager replaceTextStorage: newStorage];

由于NSTextStorage是NSMutableAttributedString的子类,你可以与所有的相同方法操纵它。



文章来源: Proper way to replace NSTextStorage in NSTextView?