3pillarlabs/ios-autocomplete

Name: ios-autocomplete

Owner: 3Pillar Global Open Source

Description: null

Created: 2015-08-14 08:03:13.0

Updated: 2018-02-07 12:04:03.0

Pushed: 2018-03-29 07:54:30.0

Homepage: null

Size: 3629

Language: Objective-C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

AutoCompletion

Description

A framework which provides text field suggestions as a dropdown list. It is available with iOS 9 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.

Installation
CocoaPods

Run Terminal

init
form :ios, '9.0'

et 'YourProjectName' do
e_frameworks!
pod 'AutoCompletion'

Carthage

Run Terminal

ub "3pillarlabs/ios-autocomplete"
hage update
Usage

1. Import AutoCompletion framework. Objective-C

ort AutoCompletion;

Swift

rt 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

s AutoCompletionDataSource: NSObject, AutoCompletionTextFieldDataSource {
@IBOutlet weak var autoCompletionTextField: AutoCompletionTextField!

override func viewDidLoad() {
    super.viewDidLoad()
    autoCompletionOutlet.suggestionsResultDataSource = self
}
// MARK: DataSource

func fetchSuggestions(forIncompleteString incompleteString: String!,
                      withCompletionBlock completion: FetchCompletionBlock!) {
    ...code...
}

Objective-C

erface AutoCompletionDataSource: NSObject <AutoCompletionTextFieldDataSource>


lementation AutoCompletionDataSource
oid)fetchSuggestionsForIncompleteString:(NSString*)incompleteString
                    withCompletionBlock:(FetchCompletionBlock)completion {
...code...





CompletionTextField.suggestionsResultDataSource = [AutoCompletionDataSource new];

4. Optionally, you can set the suggestionsResultDelegate in order to handle the suggestion selection.

Swift

s ViewController: UIViewController, AutoCompletionTextFieldDelegate {    
@IBOutlet weak var autoCompletionTextField: AutoCompletionTextField!

override func viewDidLoad() {
    super.viewDidLoad()
    autoCompletionTextField.suggestionsResultDelegate = self
}

// MARK: Delegate

func textField(_ textField: AutoCompletionTextField!, didSelectItem selectedItem: Any!) {
    ...code...
}

Objective-C

erface ViewController: UIViewController <AutoCompletionTextFieldDelegate>

perty (weak, nonatomic) IBOutlet AutoCompletionTextField *autoCompletionTextField;



lementation ViewController

oid)viewDidLoad {
[super viewDidLoad];
self.autoCompletionTextField.suggestionsResultDelegate = self;


gma mark - Delegate

oid)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

s AutoCompletionAnimation: NSObject, AutoCompletionAnimator {

func showSuggestions(for textField: AutoCompletionTextField!,
                     table: UITableView!, numberOfItems count: Int) {
    ...code...    
 }

func hideSuggestions(for textField: AutoCompletionTextField!,
                     table: UITableView!) {
    ...code...
}




CompletionTextField.animationDelegate = AutoCompletionAnimation()

Objective-C

erface AutoCompletionAnimation: NSObject<AutoCompletionAnimator>


lementation AutoCompletionAnimation

oid)showSuggestionsForTextField:(AutoCompletionTextField*)textField
                          table:(UITableView*)table
                  numberOfItems:(NSInteger)count {
...code...

oid)hideSuggestionsForTextField:(UITextField*)textField
                          table:(UITableView*)table {
...code...






CompletionTextField.animationDelegate = [AutoCompletionAnimation new];


In the demo branch you're able to see a workable example for AutoCompletion where the suggestions results come from three different scenarios: CoreData, JSON, and API.

Known issues

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.

License

AutoCompletion is released under MIT license. See LICENSE for details.

About this project

3Pillar Global

AutoCompletion is developed and maintained by 3Pillar Global.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.