Hello. I am working on a cocoa-based text editor. Should I base it on NSTextView or is there a more efficient option? Keep in mind that I plan to support tabs so there can be many editors open at the same time.
NSTextView is probably the simplest way to go if you want to get a ton of nice features for free. It can't do everything, but it's an awesome start.
I am working on a cocoa-based text editor. Should I base it on NSTextView
Yes.
or is there a more efficient option?
No, assuming “efficiency” includes your own time and effort weighed against the feature set you want to support—Cocoa's text system does a lot for you, which you'd be throwing away if you rolled your own.
Some examples:
- Undo support
- Advanced editing (emacs keys)
- Support for input managers/input methods
- Support for all of Unicode
- Mouse selection
- Keyboard selection
- Multiple selection
- Fonts
- Colors
- Images
- Sounds
- Find
- Find and Replace
- Spelling-checking
- Grammar-checking
- Text replacement
- Accessibility
If you roll your own, you get to spend months reinventing and debugging some if not most if not all of those wheels. I call that inefficient.
The text system you already have, meanwhile, is fast nearly all of the time. You need huge texts with long lines (or maybe lots of embedded images/sounds) to bog it down.
Keep in mind that I plan to support tabs so there can be many editors open at the same time.
Unless the user is going to be typing into all of them at once, I don't see how that will cause a performance problem. 0% CPU × N or N-1 views = 0% CPU.
The one place where you might have a problem is memory usage, if the documents are both many and large. They'd have to be both in the extreme, as even a modest Mac nowadays has 1 GiB of RAM, and text doesn't weigh much.
If that's the case, then you could only keep the N most recently used unmodified texts in memory, and otherwise remember only the arrays of selection ranges. But 99% of the time, swapping texts in and out will be far more expensive than just leaving them all in memory.