在NSTextView示例中使用NSTextList的项目符号列表?

Mel*_*ius 8 macos cocoa nstextview swift

我想在NSTextView中解析用户输入,以便以" - "开头的行会自动启动项目符号列表.我需要对NSTextView.textStorage做什么来启用项目符号列表,以便在每个项目符号行后按Enter时自动显示下一个项目符号?

我已经找到了一些例子,但他们都手工插入子弹,所以我想知道let textList = NSTextList(markerFormat: "{box}", options: 0)如果你必须自己手动插入盒子符号是什么意思?

目前我正在尝试在一个自定义的NSTextView中实现这一点,并且重写shouldChangeTextInRange(affectedCharRange: NSRange, replacementString: String?)因此,一个简单的例子可以解决" - "启动一个自动项目符号列表的最简单的输入情况,将受到高度重视.

更新:

这是我目前使用的代码:

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
}
Run Code Online (Sandbox Code Playgroud)

我只是想解决用户输入" - "作为NSTextView中第一个字符的最简单的情况.这是上面的代码: 在自定义NSTextView中输出

开头的较大字体来自我设置的typingAttributes.如您所见,这会自动被覆盖.

else if子句返回false 并直接插入\n将阻止NSTextView自动添加新项目符号.我第一次true从那里返回后,自动添加新子弹而不调用此方法???

Ram*_*Ram -1

尝试通过替换使用 Unicode 字符添加项目符号"- " with \u{2022}

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