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.
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.
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
How to Calculate a Distance Between Two Anchorentities
Cloudkit - What to Do When a User Adds, Modifies or Deletes an Object While Offline
Swift 3 Cocoa: Use Quicklook to Preview File in Os X
Prefix(_ Maxlength:) Is Type-Erased When Used with a Struct That Conforms to Lazysequenceprotocol
Add Custom .Colornames to UIcolor Somehow
Heightanchor.Constraint Not Change Height of View
Changing Associated Value of Enum Swift
Arkit: How to Tell If User's Face Is Parallel to Camera
Prepareforsegue from a UIbutton in a Custom Prototype Cell
Swift: Method Overriding in Parameterized Class
Execute Code on Main Thread from Async F#
Swift Protocol That Is Using an Enum with Generic Associated Type
Uipickerviewdelegate Xcode 8 Swift 3
.Childadded Observer Doesn't Get Called If The Function Is Not Called Explicitly. Swift 4