A framework which provides text field suggestions as a dropdown list. It is available with iOS 8 and later, Objective-C or Swift.
Project Rationale
The purpose of this framework is to provide a simple, yet very useful feature - autocomplete textfield. IOS autocomplete is an iOS module that behaves like a normal text field with the added functionality that it provides suggestions to the user in a dropdown.
Add the "AutoCompletion.framework" to your project. Make sure the framework is added to 'Embedded Binaries' list from the General Tab of the Project Settings.
1. Import AutoCompletion framework. Objective-C
@import AutoCompletion;
Swift
import AutoCompletion
2. Add an autocompletion instance inside a view:
2.1 Drag and drop a UITextField instance in Interface Builder.
2.2 Change it's class to AutoCompletionTextField.
2.3 Aditionally, you can customize the text field from Interface Builder.
2.4 Create a reference of the AutoCompletionTextField.
3. Set the suggestionsResultDataSource. The data source needs to conform to the AutoCompletionTextFieldDataSource protocol:
Swift
class AutoCompletionDataSource: NSObject, AutoCompletionTextFieldDataSource {
func fetchSuggestionsForIncompleteString(incompleteString: String!, withCompletionBlock completion: FetchCompletionBlock!) {
...code...
}
}
...
autoCompletionTextField.suggestionsResultDataSource = AutoCompletionDataSource()
Objective-C
@interface AutoCompletionDataSource: NSObject <AutoCompletionTextFieldDataSource>
@end
@implementation AutoCompletionDataSource
- (void)fetchSuggestionsForIncompleteString:(NSString*)incompleteString
withCompletionBlock:(FetchCompletionBlock)completion {
...code...
}
@end
...
autoCompletionTextField.suggestionsResultDataSource = [AutoCompletionDataSource new];
4. Optionally, you can set the suggestionsResultDelegate in order to handle the suggestion selection.
Swift
class ViewController: UIViewController, AutoCompletionTextFieldDelegate {
@IBOutlet weak var autoCompletionTextField: AutoCompletionTextField!
override func viewDidLoad() {
super.viewDidLoad()
autoCompletionTextField.suggestionsResultDelegate = self
}
// MARK: Delegate
func textField(textField: AutoCompletionTextField!, didSelectItem selectedItem: AnyObject!) {
...code...
}
}
Objective-C
@interface ViewController: UIViewController <AutoCompletionTextFieldDelegate>
@property (weak, nonatomic) IBOutlet AutoCompletionTextField *autoCompletionTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.autoCompletionTextField.suggestionsResultDelegate = self;
}
#pragma mark - Delegate
- (void)textField:(AutoCompletionTextField*)textField didSelectItem:(id)selectedItem {
...code...
}
5. Also, you can set the animationDelegate in order to provide a custom animation to displaying of the results. It needs to conform to the AutoCompletionAnimator protocol:
Swift
class AutoCompletionAnimation: NSObject, AutoCompletionAnimator {
func showSuggestionsForTextField(textField: AutoCompletionTextField!, table: UITableView!, numberOfItems count: Int) {
...code...
}
func hideSuggestionsForTextField(textField: AutoCompletionTextField!, table: UITableView!) {
...code...
}
}
...
autoCompletionTextField.animationDelegate = AutoCompletionAnimation()
Objective-C
@interface AutoCompletionAnimation: NSObject<AutoCompletionAnimator>
@end
@implementation AutoCompletionAnimation
- (void)showSuggestionsForTextField:(AutoCompletionTextField*)textField
table:(UITableView*)table
numberOfItems:(NSInteger)count {
...code...
}
- (void)hideSuggestionsForTextField:(UITextField*)textField
table:(UITableView*)table {
...code...
}
@end
...
autoCompletionTextField.animationDelegate = [AutoCompletionAnimation new];
In the demo project you're able to see a workable example for AutoCompletion where the suggestions results come from three different scenarios: CoreData, JSON, and API.
If written text and suggestion have different letter case or if written text starts shifting to left, the suggestion doesn't overlap correctly the written text.
AutoCompletion is released under MIT license. See LICENSE for details.
![3Pillar Global] (http://www.3pillarglobal.com/wp-content/themes/base/library/images/logo_3pg.png)
AutoCompletion is developed and maintained by 3Pillar Global.