简体   繁体   English

NSTextView setFormatter?

[英]NSTextView setFormatter?

With a NSTextField we can easially call setFormatter if we want to say for instance create a custom formatter that only allows numbers (take this question for example). 使用NSTextField我们可以轻松调用setFormatter如果我们想要创建一个只允许数字的自定义格式化程序(例如以此问题为例)。

But what if we want to apply a formatter to a NSTextView ? 但是,如果我们想将格式化程序应用于NSTextView呢? There does not seem to be a way to set a formatter, unless I am overlooking something. 似乎没有办法设置格式化程序,除非我忽略了某些东西。

Any suggestions on how to get around this? 有关如何绕过这个的任何建议?

An NSFormatter is designed for the format of a single line string. NSFormatter设计用于单行字符串的格式。 This intention matches against NSTextField which would usually show a single line (particularly if using a formatter). 此意图与NSTextField匹配,后者通常显示单行(特别是如果使用格式化程序)。

The point of a formatter is both to format existing text for display and to validate text entry from a user. 格式化程序的目的是格式化现有文本以进行显示并验证用户的文本输入。 You can use a formatter with an NSTextView to perform both of these actions, you just need to do a little work yourself to process text before you add it to the view ( stringForObjectValue: ) or when a user enters it ( textView:shouldChangeTextInRange:replacementString: and isPartialStringValid:newEditingString:errorDescription: ). 您可以使用带有NSTextView的格式化程序来执行这两个操作,您只需要自己做一些工作来处理文本,然后再将其添加到视图( stringForObjectValue:或用户输入文本时( stringForObjectValue: textView:shouldChangeTextInRange:replacementString:isPartialStringValid:newEditingString:errorDescription:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM