如何以编程方式将项目符号列表添加到 NSTextView
这个问题可能听起来很奇怪,但我已经为此苦苦挣扎了几天。
我有一个 NSTextView,可以显示一些带有一些格式选项的文本。其中之一是能够打开/关闭所选内容或当前行的项目符号列表(最简单的列表)。
我知道 NSTextView 上有一个 orderFrontListPanel: 方法,它打开带有可用列表参数的窗口以供选择和编辑(就像在 TextView 中按菜单 -> 格式 -> 列表...)。 我已经弄清楚并实现了手动添加项目符号,并且 NSTextView 似乎与它们的行为几乎正确。我所说的“几乎”是指它保留制表符位置,在“输入”时继续列表,等等。但是有一些小故障不适合我,并且与标准实现不同。
我试图找到以编程方式设置列表的默认方法,就像通过“列表...”菜单完成的那样,但没有成功。
我寻求帮助,每一点信息都将不胜感激:)。
PS:我查看了 TextView 源代码,发现了很多有趣的内容,但没有迹象或线索如何以编程方式启用列表。
更新
仍在调查。我发现当您将 orderFrontListPanel: 发送到 NSTextView 然后选择项目符号并按 Enter 键时,不会向 NSTextView 发送任何特殊消息。这意味着项目符号列表可以在此弹出面板内的某个位置构建并直接设置到 TextView 的文本容器...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以编程方式将项目符号列表添加到 NSTextView 的两种方法:
方法 1:
以下链接引导我使用第一种方法,但它是不必要的迂回,除非您想对项目符号使用一些特殊的非 Unicode 字形:
这需要:(1) 一个子类化的布局管理器,它替代某些任意字符的项目符号符号; (2) 带有firstLineHeadIndent的段落样式,一个比该缩进稍大的制表位,以及将两者结合起来的换行的headIndent。
布局管理器看起来像这样:
将布局管理器分配给窗口/视图控制器的 awakeFromNib 中的 textview,如下所示:
然后添加一个类似这样的方法:
测试一下:
你会得到这个:
方法 2:
但项目符号是常规 Unicode 字符,十六进制 2022。因此您可以将其放入直接输入字符串,并获得精确的测量值,如下所示:
因此不需要自定义布局管理器。只需如上所述设置 paragStyle 缩进,并将文本字符串附加到包含行 return + 项目符号字符 + 空格(或 + tab,在这种情况下您仍然需要该制表位)的字符串。
使用空格,会产生更紧凑的结果:
想要使用项目符号以外的字符吗?这是一个很好的 Unicode 图表:http://www.danshort.com/unicode/
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/