停止 NSTokenField 修剪空格

Stop NSTokenField from trimming whitespace

我正在尝试将 stringValue 设置为 NSTokenField(NSTextField 的子类),但我发现如果字符串以 space 开头,它会将其删除。我想保留 space。我也尝试过应用格式化程序,但没有成功。即:

tokenField.stringValue = " 1"

这实际上会在字段中读出“1”。

此外,我想格式化一个整数,但还要在它前面保留零。即:

    tokenField.stringValue = "001"

这也将在字段中读出“1”。

我试过重写各种函数,但没有成功。有什么想法吗?

谢谢!

来自the documentation of NSTokenField

In OS X v10.4, NSTokenField trims whitespace around tokens but it does not trim whitespace in macOS versions 10.5.0 and 10.5.1. In OS X v10.5.2, you get whitespace-trimming behavior by either linking against the v10.4 binary or linking against the v10.5 binary and not implementing the tokenField(_:representedObjectForEditing:) method. If you do not want the whitespace-trimming behavior, link against the v10.5 binary and implement this method, returning the editing string if you have no represented object.

好的,它有点旧,方法名称不正确,但它说:

If you do not want the whitespace-trimming behavior, implement tokenField(_:representedObjectForEditingString:), returning the editing string if you have no represented object.