Proper way to replace NSTextStorage in NSTextView?

家住魔仙堡 提交于 2019-12-04 18:56:54

问题


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:]

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.

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

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


回答1:


You can do something like this to change the storage for an 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.



来源:https://stackoverflow.com/questions/15527305/proper-way-to-replace-nstextstorage-in-nstextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!