可可:在空格键停止从字段编辑器自动完成 [英] Cocoa: Stopping the field editor from auto-completing on space key
问题描述
我有几个的NSTextField
控制自定义视图,我想提供定制的自动补全,我已经成功地实现所有使用 NSTextFieldDelegate
协议。自动补全是全名或地名,这取决于文本字段进行编辑。
I have a custom view with several NSTextField
controls for which I want to supply custom auto-completions and I have successfully implemented all that using the NSTextFieldDelegate
Protocol. The auto-completions are full names or place names, depending on which text field is being edited.
问题是,自动补全,几乎总是包含空格字符,因此,如果用户键入一个匹配的建议,但并不想接受这一建议的东西,字段编辑器将接受该建议,当用户presses空格键。我想字段编辑器只能使用Tab键接受该建议。
The issue is that the auto-completions almost always contain a space character and so if the user is typing something that matches a suggestion, but doesn't want to accept that suggestion, the field editor will accept the suggestion when user presses the space key. I want the field editor to accept the suggestion using the tab key only.
据我所知,这将涉及到子类 NSTextView
来提供一个自定义字段编辑器,这是<一个href=\"https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextEditing/Tasks/Subclassing.html#//apple_ref/doc/uid/20000937-CJBJHGAG\"相对=nofollow>苹果记载的作为是可以接受的方式,但它不是我清楚我需要重写哪些方法和哪些覆盖的方法需要做的,为了得到我我想要的。
I understand that this will involve subclassing NSTextView
to provide a custom field editor, and this is documented by Apple as being the acceptable way, however it's not clear to me what methods I need to override and what the overridden methods need to do, in order to get me what I want.
任何人都可以表明这是如何实现的?
Can anyone suggest how this is achieved?
推荐答案
我承认我被这个问题前面摸索了一段时间,在此之前的I发现通过谷歌福可接受的答案的神奇code,从原来的回答者公然被盗:
I admit that I'd been fumbling with this question for quite some time earlier, before I discovered an acceptable answer via Google-fu. The magic code, blatantly stolen from the original answerer:
@interface MLFieldEditor : NSTextView @end
@implementation MLFieldEditor
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag {
// suppress completion if user types a space
if (movement == NSRightTextMovement) return;
// show full replacements
if (charRange.location != 0) {
charRange.length += charRange.location;
charRange.location = 0;
}
[super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];
if (movement == NSReturnTextMovement)
{
[[NSNotificationCenter defaultCenter] postNotificationName:MLSearchFieldAutocompleted object:self userInfo:nil];
} }
@end
(<一个href=\"http://stackoverflow.com/questions/3981439/how-to-$p$pvent-nssearchfield-from-overwriting-entered-strings-using-the-first-au\">Additional参考)
这篇关于可可:在空格键停止从字段编辑器自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!