0

I've successfully created/modified/implemented (with a lot of help) the below to enable an autocompletion text view from an array of suggestions. It's case-insensitive and works really well! The only problem is that the spacebar selects the first suggestion even if it's not the one I want... That is, when typing "Joe" I get a list including "Joe Client" "Joe Smith" "Joe John Smith" etc... if I press the spacebar in an attempt to further refine it (let's say I want "Joe John Smith") defaults to selecting the first suggestion "Joe Client" :(

I'd imagine that there's some way of overriding the space so that it doesn't act as an "enter" or "tab" but clearly I'm not sure how to do that... Or something similar to the way the backspace/delete key is treated... Any insight or suggestions would be extremely helpful.

Thanks in advance.

autocompleteController.h:

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface autocompleteController : NSViewController< NSTextFieldDelegate, NSControlTextEditingDelegate>

@property (weak) IBOutlet NSTextField *textField;
@property (nonatomic) BOOL isAutocompleting;
@property (nonatomic, strong) NSString * lastEntry;
@property (nonatomic) BOOL backspaceKey;

-(IBAction)onEnter:(id)sender;
@end
@interface NSString (autocomplete)

- (BOOL)hasPrefixIgnoringCase:(NSString*)aString;
@end;

autocompleteController.M:

#import "autocompleteController.h"
#import "AddressBook/AddressBook.h"

@implementation autocompleteController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField.delegate = self;
}   
-(void)controlTextDidChange:(NSNotification *)obj{
    NSTextView * fieldEditor = [[obj userInfo] objectForKey:@"NSFieldEditor"];
    if (self.isAutocompleting == NO  && !self.backspaceKey) {
        self.isAutocompleting = YES;
        self.lastEntry = [[fieldEditor string] copy];
        [fieldEditor complete:nil];
        self.isAutocompleting = NO;
    }
    if (self.backspaceKey) {
        self.backspaceKey = NO;
    }
}   
-(NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index{
    NSMutableArray * suggestions = [NSMutableArray array];

    //A whole bunch of code to build the array of suggestions goes here- it's irrelevant to the question and long so in the name of brevity I've removed it...

    return suggestions;
}   
-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector{
    if (commandSelector == @selector(deleteBackward:)) {
        self.backspaceKey = YES;
    }
    return NO;
}
@end  
@implementation NSString (autocomplete)    
- (BOOL)hasPrefixIgnoringCase:(NSString*)aString
{
    NSRange prefix = [self rangeOfString:aString options:NSCaseInsensitiveSearch];
    return prefix.location == 0 && prefix.length == aString.length;
}
@end
julian
  • 157
  • 1
  • 12
  • Google found this: [Stopping the field editor from auto-completing on space key](http://stackoverflow.com/questions/9819378/cocoa-stopping-the-field-editor-from-auto-completing-on-space-key) – Willeke Jan 02 '16 at 13:02
  • Yeah I saw that and a couple of other posts on SO but am not exactly sure how to implement that... I guess it's a bit beyond me at the moment... – julian Jan 02 '16 at 18:55
  • I can repeat the answers from the other questions but that's not going to help either. – Willeke Jan 02 '16 at 20:54

0 Answers0