简体   繁体   English

在NSTextView中替换NSTextStorage的正确方法?

[英]Proper way to replace NSTextStorage in 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. 目前我需要非常频繁和精确的线路处理能力,所以我想NSTextStorage类。 But I couldn't find any method to set a new text storage to NSTextView . 但是我找不到任何方法来为NSTextView设置新的文本存储。 The only method I could find was 我能找到的唯一方法是

-[NSLayoutManager replaceTextStorage:]

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. 因为它似乎只是替换链接的NSLayoutManagers而不是NSTextView的文本存储。

I also considered subclassing NSTextView and overriding -textStorage method, but if the class is not designed for subclassing, it will make undefined result. 我还考虑了子类化NSTextView和重写-textStorage方法,但是如果该类不是为子类设计的,那么它将产生未定义的结果。

Anyone has tried to use custom NSTextStorage on NSTextView? 有人试图在NSTextView上使用自定义NSTextStorage吗? How can I do this? 我怎样才能做到这一点? Or is this prohibited by design? 或者这是否被设计禁止?

You can do something like this to change the storage for an NSTextView: 您可以执行以下操作来更改NSTextView的存储空间:

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

[aTextView.layoutManager replaceTextStorage: newStorage];

Since NSTextStorage is a subclass of NSMutableAttributedString you can manipulate it with all of the same methods. 由于NSTextStorage是NSMutableAttributedString的子类,因此您可以使用所有相同的方法对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM