Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Insert" Mode #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions HTTextFieldAutocompletionExample/HTAutocompleteManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ + (HTAutocompleteManager *)sharedManager
}

#pragma mark - HTAutocompleteTextFieldDelegate
-(BOOL)textFieldShouldReplaceCompletion:(HTAutocompleteTextField *)textField {
if (textField.autocompleteType == HTAutocompleteTypeColor){
return YES;
}
return NO;
}

- (NSString *)textField:(HTAutocompleteTextField *)textField
completionForPrefix:(NSString *)prefix
Expand Down Expand Up @@ -311,9 +317,8 @@ - (NSString *)textField:(HTAutocompleteTextField *)textField

if ([stringToCompare hasPrefix:stringToLookFor])
{
return [stringFromReference stringByReplacingCharactersInRange:[stringToCompare rangeOfString:stringToLookFor] withString:@""];
return stringFromReference;
}

}
}

Expand Down
3 changes: 1 addition & 2 deletions HTTextFieldAutocompletionExample/HTAutocompleteTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
- (NSString*)textField:(HTAutocompleteTextField*)textField
completionForPrefix:(NSString*)prefix
ignoreCase:(BOOL)ignoreCase;

- (BOOL)textFieldShouldReplaceCompletion:(HTAutocompleteTextField*)textField;
@end

@protocol HTAutocompleteTextFieldDelegate <NSObject>

@optional
- (void)autoCompleteTextFieldDidAutoComplete:(HTAutocompleteTextField *)autoCompleteField;
- (void)autocompleteTextField:(HTAutocompleteTextField *)autocompleteTextField didChangeAutocompleteText:(NSString *)autocompleteText;

@end

@interface HTAutocompleteTextField : UITextField
Expand Down
20 changes: 20 additions & 0 deletions HTTextFieldAutocompletionExample/HTAutocompleteTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,27 @@ - (void)forceRefreshAutocompleteText
}

#pragma mark - Accessors
@synthesize autocompleteString = _autocompleteString;

-(NSString *)autocompleteString {
id<HTAutocompleteDataSource> datasource;
if([self.autocompleteDataSource respondsToSelector:@selector(textFieldShouldReplaceCompletion:)]) {
datasource = self.autocompleteDataSource;
} else if([DefaultAutocompleteDataSource respondsToSelector:@selector(textFieldShouldReplaceCompletion:)]) {
datasource = DefaultAutocompleteDataSource;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that this will be a confusing behavior, where essentially the HTAutocompleteTextField can have two data sources. Is there any way that the goal can be accomplished with a single data source?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that having a DefaultAutocompleteDataSource is fairly confusing in and of itself, partially because its unconventional.


if([datasource textFieldShouldReplaceCompletion:self]) {
NSStringCompareOptions *options = self.ignoreCase? NSCaseInsensitiveSearch : 0;

if([_autocompleteString rangeOfString:self.text options:options].location == 0){
return [_autocompleteString substringFromIndex:self.text.length];
} else {
return @"";
}
}
return _autocompleteString;
}
- (void)setAutocompleteString:(NSString *)autocompleteString
{
_autocompleteString = autocompleteString;
Expand Down