Note that all of the content on this page, including the note below this one, no longer applies. If you want to implement views with line numbers, I recommend Shysaur‘s Fragaria fork, which is updated with modern Objective-C code, compatible with your Swift projects, and offers a lot of great functionality in addition to line numbers. It's the editor that powers Balthisar Tidy.

Note that there used to be content here called JSDTextView, but that is no more. Instead we’ve adopted JanX2’s fork of NoodleKit’s NoodleLineNumberView. We recommend that you no longer attempt to adapt our previous solution to modern macOS, and use Noodlekit or, even better, our new NSTextView+JSDExtensions which implements it for you!

NSTextView+JSDExtensions

About

NSTextView+JSDExtensions is a category to support NSTextView with some extra features:

  1. The ability to toggle word wrap.
  2. Simple line and character highlighting.
  3. The display of logical line numbers in the view.

What’s Involved

property highlitLine
Highlight this row number (0 for none).
property highlitColumn
Highlight this column of the row (0 for none).
property showsHighlight
Sets/Indicates the current highlight state.
property WordwrapsText
Sets/Indicates the current wordwrap state.
property showsLineNumbers
Sets/Indicates whether or not line numbers appear.
scrollLineToVisible:Line
Ensures that a logical line is visible in the view.
highlightLine:Column
As above, including scrolling into view.

Why This is For You

You’ll be able to very simply implement some great features in your Cocoa programs without reinventing the wheel!