禁用 NSTextField 中的 Option-Enter 换行符?
我想禁止在 NSTextField 中输入换行符,默认情况下可以通过键入 Option-Enter(或 Option-Return)来实现。
虽然我发现一些 页面解释如何允许换行而不必须使用选项修饰符,我找不到任何可以完全禁用换行符的东西。
如何才能做到这一点?
I want to disallow entering a new-line character in my NSTextField, which by default is possible by typing Option-Enter (or Option-Return).
While I found some pages explaining how to allow line breaks without having to use the Option modifier, I couldn't find anything for disabling line breaks altogether.
How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建 NSFormatter 的子类并实现 isPartialStringValid:... 方法来阻止换行符。
You could create a subclass of NSFormatter and implement the isPartialStringValid:... method to block the newline.
我执行以下代码来防止使用 Control、Option + Enter(回车)时出现换行问题。
I do the following code for preventing from line-break issue in case of using Control, Option + Enter (Return).
请问您为什么要禁用此功能? 这是操作系统的一个非常标准的部分。 请记住,用户很可能从其他地方粘贴一些包含换行符的字符串,然后他们将根据需要进行编辑。 保留此功能不变也许会更好,但当需要将值从视图复制到模型时,使用 NSFormatter 删除换行符后的所有内容?
May I ask why you wish to disable this feature? It's a pretty standard part of the OS. Bear in mind that the user might well paste in some string from elsewhere that contains a line break and they'll then edit to their needs. Would it perhaps be better to leave this feature intact, but use an NSFormatter to strip out everything after the line break when it is time to copy the value out of the view to the model?