Reputation: 95
It is there a solution to have a completion of a NSTextField with method :
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int*)index
with several words and not one? Because when you type a space, the completion start again...
Thanks.
Upvotes: 1
Views: 1301
Reputation: 3481
Better late than never, it might be helpful for others:
It's a little tricky problem, since NSControlTextEditingDelegate
/ NSTextFieldDelegate
doesn't offer a way to solve it directly. What you need to do is creating a custom subclass of NSTextView
(yes, text view), and override the method - (NSRange)rangeForUserCompletion
:
- (NSRange)rangeForUserCompletion
{
return [self selectedRange];
}
And then subclass NSTextFieldCell
to override the method - (NSTextView *)fieldEditorForView:
:
- (NSTextView *)fieldEditorForView:(NSView *)aControlView
{
static MyTextView* _myFieldEditor = nil;
if (_myFieldEditor == nil) {
_myFieldEditor = [[MyTextView alloc] init];
[_myFieldEditor setFieldEditor:YES];
}
return _myFieldEditor;
}
Then in Interface Builder, set the class of your text field's cell to your subclass of NSTextFieldCell
. What will happen is when your text field becomes first responder, the window will call your cell's -fieldEditorForView:
method, and use your custom text view as the field editor. So during editing the value of your text field, any completion will call -(NSRange)rangeForUserCompletion
on your text view.
Then you can fine tune your -rangeForUserCompletion
to make it return the exact range you want for the completion.
Also, the code in fieldEditorForView:
assumes that your app uses only one window, if you are using multiple windows (e.g. document-based apps), you'll have to change it and keep one field editor instance per window.
Hope it helps :)
Upvotes: 4