Swift & Nstextfield: How to Work with Text Completion

Swift & NSTextField: How to work with Text Completion

NSTextField doesn't implement complete:, NSTextView does. NSTextField uses a NSTextView, the field editor, to edit its contents, see Text Fields, Text Views, and the Field Editor. Use currentEditor() to get the field editor from the text field.

@IBAction func completeButton(_ sender: NSButton) {
inputField.currentEditor()?.complete(nil)
}

NSTextField autocomplete

Instead of having the boolean property doingAutocomplete, make the property your control that made the request. Let's call it autoCompleteRequestor:

@property (strong) NSControl* autoCompleteRequestor;

So where you set your current property doingAutocomplete to YES, instead store a reference to your control.

- (void)controlTextDidChange:(NSNotification *)obj
{
if([obj object] == self.searchField)
{
[self.spinner startAnimation:nil];
[self.wordcompletionStore completeString:self.searchField.stringValue];

if(self.autoCompleteRequestor)
return;
else
{
self.autoCompleteRequestor = [[obj userInfo] objectForKey:@"NSFieldEditor"];
}
}
}

Now when your web request is done, you can call complete: on your stored object.

- (void) completionStore:(WordcompletionStore *)store didFinishWithWords:(NSArray *)arrayOfWords
{
[self.spinner stopAnimation:nil];
self.completions = arrayOfWords;
if (self.autoCompleteRequestor)
{
[self.autoCompleteRequestor complete:nil];
self.autoCompleteRequestor = nil;
}
}

NSTextField with auto-suggestions like Safari's address bar?

This only addresses half of your answer, but I believe you need to subclass NSTextView and implement the - (NSRange)rangeForUserCompletion method, returning the range of the entire string in the text field. This should make sure that it doesn't just autocomplete the most recently entered word.

If you want a custom menu, you're going to have to do that yourself, probably by implementing the -controlTextDidChange: method and displaying a custom view with a table when appropriate.

Execute callback when text changes inside a NSTextField in Swift

The reason you are crashing is that your selector is wrong. It should be selector: "fieldTextDidChange:" (notice the final colon).

Deselecting Text in NSTextField

In a simple app with just a window and textfield.

I set the textField to firstResponder.

This makes the text selected when the app runs.

Sample Image

If I add to the the applicationDidFinishLaunching

NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];

Now when run the Text is not selected and the insertion point is at the end of the text.

Sample Image

I used selectedRange to get the selection char length. As the text was selected in the first place and seem more simple.

- (void)awakeFromNib{
[_theTextField setStringValue:@"100000"];

}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[[NSApplication sharedApplication] mainWindow] makeFirstResponder:_theTextField];

NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];

}



Related Topics



Leave a reply



Submit