13

I would want to parse user input in a NSTextView, so that a line beginning with "- " would automatically start a bulleted list. What do I have to do to the NSTextView.textStorage to enable a bulleted list so that the next bullet automatically appears when you press Enter after each bullet line?

I've found a few examples but they all insert the bullets by hand, so I was wondering what's the point of specifying let textList = NSTextList(markerFormat: "{box}", options: 0) if you then have to manually insert the box-symbol yourself?

Currently I'm trying to implement this in a custom NSTextView with an overriden shouldChangeTextInRange(affectedCharRange: NSRange, replacementString: String?) So a simple example that would solve the simplest input case of "- " starting an automatic bulleted list, would be highly regarded.

Update:

Here is the code I'm currently using:

override func shouldChangeTextInRange(affectedCharRange: NSRange, replacementString: String?) -> Bool {
   super.shouldChangeTextInRange(affectedCharRange, replacementString: replacementString)

   if string == "-" && replacementString == " " {
      let textList = NSTextList(markerFormat: "{disc}", options: 0)
      let textListParagraphStyle = NSMutableParagraphStyle()
      textListParagraphStyle.textLists = [textList]
      let attributes = [NSParagraphStyleAttributeName: textListParagraphStyle]
      string = "\t\(textList.markerForItemNumber(0))\t"
      textStorage?.addAttributes(attributes, range: NSMakeRange(0, textStorage!.string.length))
      return false
   }
   else if affectedCharRange.location > 0 && replacementString == "\n\n" {
      textStorage?.insertAttributedString(NSAttributedString(string: "\t"), atIndex: affectedCharRange.location)
      return true
   }

   return true
}

I'm just trying to solve the simplest case of the user typing "- " as the first characters in the NSTextView. This is what happens with the code above: Output in custom NSTextView

The bigger font in the beginning comes from the typingAttributes I have set. This gets automatically overriden later as you can see.

Returning false from the else if clause and inserting the \n directly there will prevent the NSTextView from automatically adding new bullets. After the first time I return true from there, new bullets are added automatically without calling this method???

Melodius
  • 2,505
  • 3
  • 22
  • 37

1 Answers1

-1

Try to add bullets using Unicode character by replace "- " with \u{2022}

textView.text = " \u{2022} This is a list item!

Ram Vadranam
  • 485
  • 5
  • 14
  • 1
    I'm not trying to merely insert " • " in place of " -", which will of course look like the beginning of a list, but it doesn't create a NSTextList into the textView and thus it lacks all the editing functionality that a properly recognized list-object gives for free. If you press enter at the end of the first item, the next item beginning with a bullet won't be added by the textView. A correct answer will need to tell how to properly insert a NSTextList into the NSTextView. – Melodius Apr 05 '16 at 15:23