I'm trying to figure out how to use NSTextList but have found little useful information online aside from this SO question and the comment in this blog.
Using this I have been able to create a crude example to figure it out:
let textView = NSTextView()
//Create NSTextList
let list = NSTextList(markerFormat: .decimal, options: 0)
list.startingItemNumber = 1
//Create NSParagraphStyle with text list
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.textLists = [list]
let attributes = [NSAttributedString.Key.paragraphStyle : paragraphStyle]
//Add attributes to list block
let nsmutableAttributedString = NSMutableAttributedString(string: "\t\(list.marker(forItemNumber: 1))\tList Item 1 ", attributes: attributes)
textView.textStorage?.setAttributedString(nsmutableAttributedString)
return textView
This works well and when the app is run, pressing enter
creates a new properly formatted line and pressing tab
indents the list item correctly.
All good... However, the spacing around the marker seems to be incorrect and doesn't reflect what you would get in TextEdit for example.
Here is TextEdit, notice the spacing around the marker
Here is my textView, notice the larger spacing around the marker which, after the procedure highlighted below, reverts to the 'default' text edit spacing...
There are three things I'm hoping to get out of this:
- Am I doing this wrong in any way? I'm pretty sure I must use
\t
around the marker to make it work. - Any ideas on how to achieve the default spacing around the marker that apple achieves across all of their text editing apps (notes, textedit, etc)
- Any general tips for NSTextList. There isn't much out there so if anyone could share their knowledge that would be greatly appreciated.