Re: Auto-complete in an NSTextView
Re: Auto-complete in an NSTextView
- Subject: Re: Auto-complete in an NSTextView
- From: Douglas Davidson <email@hidden>
- Date: Mon, 17 Apr 2006 10:25:16 -0700
On Apr 17, 2006, at 10:02 AM, Theodore H. Smith wrote:
How would I go about making some kind of auto-complete menu, in an
NSTextView?
Xcode's IDE has auto-complete, as does Safari and even Mail's "To:"
field.
I'd like to add an auto-complete pretty much like those, to an
NSTextView.
How is it done?
The examples you mention probably roll their own, but here is a
little demo I put together for WWDC a few years back. It's fairly
crude, but it at least is something to start from.
Douglas Davidson
@interface Controller : NSObject {
id myTextView; // We should be set up in the nib as this text
view's delegate.
unsigned nextInsertionIndex;
NSTimer *completionTimer;
}
- (void)doCompletion:(NSTimer *)timer;
- (void)startCompletionTimer;
- (void)stopCompletionTimer;
@end
#define COMPLETION_DELAY (0.5)
@implementation Controller
- (void)doCompletion:(NSTimer *)timer {
[self stopCompletionTimer];
[myTextView complete:nil];
}
- (void)startCompletionTimer {
[self stopCompletionTimer];
completionTimer = [[NSTimer
scheduledTimerWithTimeInterval:COMPLETION_DELAY target:self
selector:@selector(doCompletion:) userInfo:nil repeats:NO] retain];
}
- (void)stopCompletionTimer {
[completionTimer invalidate];
[completionTimer release];
completionTimer = nil;
}
/* NSTextView delegate methods */
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:
(NSRange)affectedCharRange replacementString:(NSString *)
replacementString {
/* Start a timer to fire autocompletion on the typing of "c" in
"Mac". Stop the timer on any other changes. */
if (NSOrderedSame == [@"c" compare:replacementString
options:NSCaseInsensitiveSearch]) {
[self startCompletionTimer];
nextInsertionIndex = affectedCharRange.location +
[replacementString length];
} else {
[self stopCompletionTimer];
nextInsertionIndex = NSNotFound;
}
return YES;
}
- (NSRange)textView:(NSTextView *)textView
willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange
toCharacterRange:(NSRange)newSelectedCharRange {
/* Stop the timer on selection changes other than those caused
by the typing that started the timer. */
if (newSelectedCharRange.length > 0 ||
newSelectedCharRange.location != nextInsertionIndex) [self
stopCompletionTimer];
return newSelectedCharRange;
}
- (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)
words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int
*)index {
/* Complete "Mac" to "Macintosh", "Mac OS", or "Mac OS X". */
NSArray *result = nil;
NSString *string = [[textView string]
substringWithRange:charRange];
if (NSOrderedSame == [@"Mac" compare:string
options:NSCaseInsensitiveSearch]) {
result = [NSArray arrayWithObjects:@"Macintosh", @"Mac OS",
@"Mac OS X", nil];
}
return result;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden