The question may sound strange but I've been struggling with it for a few days.
I have a NSTextView that can display some text with a few formatting options. One of them is the ability to turn on/off the bullet list (the easiest one) for a selection or current row.
I know that there is a orderFrontListPanel: method on NSTextView that opens the window with available list parameters to select from and edit (like in TextView when you press Menu->Format->List...). I have already figured out and implemented adding bullets by hand and the NSTextView seems to behave with them almost correctly. By saying almost I mean that it preserves tab positions, continues the list on 'enter', etc. But there are some minor glitches that dont's suit me and differs from standart implementation.
I tried to find the default way to set lists programmatically like it is done through 'List...' menu with no luck.
I ask for help, every little bit of information will be appreciated :).
P.S.: I have looked into the TextView source code, found a lot of interesting but no sign or clue how to enable lists programmatically.
Update
Still investigating. I found that when you send orderFrontListPanel: to your NSTextView and then select bullets and press enter, no special messages are sent to NSTextView. It means that the bullet list may be constructed somewhere inside this popup panel and set directly to TextView's text container...
Two methods of programmatically adding a bulleted list to an NSTextView:
Method 1:
The following links led me to this first method, but it’s unnecessarily roundabout unless you want to use some special non-Unicode glyph for the bullet:
This requires: (1) a subclassed layout manager that substitutes the bullet glyph for some arbitrary character; and (2) a paragraph style with a firstLineHeadIndent, a tab stop slightly bigger than that indent, and a headIndent for wrapped lines that combines the two.
The layout manager looks like this:
Assign the layout manager to the textview in your window/view controller’s awakeFromNib, like this:
And then add a method something like this:
Test it out:
You get this:
Method 2:
But the bullet is a regular Unicode char, at hex 2022. So you can put it in the string directly, and get an exact measurement, like this:
So there is no need for the custom layout manager. Just set the paragStyle indentations as above, and append your text string to a string holding the line return + bullet char + space (or + tab, in which case you’ll still want that tab stop).
Using a space, this produced a tighter result:
Want to use a character other than the bullet? Here’s a nice Unicode chart: http://www.danshort.com/unicode/