SearchTextField
Overview
SearchTextField is a subclass of UITextField, written in Swift that makes really easy the ability to show an autocomplete suggestions list.
You can decide wether to show the list as soon as the field is focused or when the user starts typing.
You can also detects when the user stops typing, very useful when you can get a suggestion list from a remote server.
New Feature!
Now you can make suggestions "inline", showing the first matched result as the placeholder (instead of the results list) and selecting it when the user touches the enter key.
Requirements
Installation
SearchTextField is available through CocoaPods. To install
it, simply add the following line to your Podfile:
use_frameworks!
pod "SearchTextField"
Manual installation
Just import SearchTextField.swift into your project
Usage
You can use it in the simplest way...
import SearchTextField
@IBOutlet weak var mySearchTextField: SearchTextField!
let mySearchTextField = SearchTextField(frame: CGRectMake(10, 100, 200, 40))
mySearchTextField.filterStrings(["Red", "Blue", "Yellow"])
...or you can customize it as you want
let item1 = SearchTextFieldItem(title: "Blue", subtitle: "Color", image: UIImage(named: "icon_blue"))
let item2 = SearchTextFieldItem(title: "Red", subtitle: "Color", image: UIImage(named: "icon_red"))
let item3 = SearchTextFieldItem(title: "Yellow", subtitle: "Color", image: UIImage(named: "icon_yellow"))
mySearchTextField.filterItems([item1, item2, item3])
mySearchTextField.theme = SearchTextFieldTheme.darkTheme()
mySearchTextField.theme.font = UIFont.systemFontOfSize(12)
mySearchTextField.theme.bgColor = UIColor (red: 0.9, green: 0.9, blue: 0.9, alpha: 0.3)
mySearchTextField.theme.borderColor = UIColor (red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
mySearchTextField.theme.separatorColor = UIColor (red: 0.9, green: 0.9, blue: 0.9, alpha: 0.5)
mySearchTextField.theme.cellHeight = 50
mySearchTextField.comparisonOptions = [.caseInsensitive]
mySearchTextField.maxNumberOfResults = 5
mySearchTextField.maxResultsListHeight = 200
mySearchTextField.highlightAttributes = [NSBackgroundColorAttributeName: UIColor.yellowColor(), NSFontAttributeName:UIFont.boldSystemFontOfSize(12)]
mySearchTextField.itemSelectionHandler = {item, itemPosition in
mySearchTextField.text = item.title
}
mySearchTextField.forceRightToLeft = true
mySearchTextField.startVisible = true
mySearchTextField.startVisibleWithoutInteraction = true
mySearchTextField.minCharactersNumberToStartFiltering = 3
mySearchTextField.forceNoFiltering = true
mySearchTextField.hideResultsList()
mySearchTextField.userStoppedTypingHandler = {
if let criteria = self.mySearchTextField.text {
if criteria.characters.count > 1 {
self.mySearchTextField.showLoadingIndicator()
self.searchMoreItemsInBackground(criteria) { results in
self.mySearchTextField.filterItems(results)
self.mySearchTextField.stopLoadingIndicator()
}
}
}
}
mySearchTextField.itemSelectionHandler = { filteredResults, itemPosition in
let item = filteredResults[itemPosition]
print("Item at position \(itemPosition): \(item.title)")
self.mySearchTextField.text = item.title
}
let header = UILabel(frame: CGRect(x: 0, y: 0, width: acronymTextField.frame.width, height: 30))
header.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
header.textAlignment = .center
header.font = UIFont.systemFont(ofSize: 14)
header.text = "Pick your option"
mySearchTextField.resultsListHeader = header
New feature: show the first matched result as placeholder (inline mode)
mySearchTextField.filterStrings(["Red", "Blue", "Yellow"])
mySearchTextField.inlineMode = true
New feature: autocomplete from, for example, a list of email domains
emailInlineTextField.inlineMode = true
emailInlineTextField.startFilteringAfter = "@"
emailInlineTextField.startSuggestingInmediately = true
emailInlineTextField.filterStrings(["gmail.com", "yahoo.com", "yahoo.com.ar"])
Swift Versions
Swift 5 supported from 1.2.3 version.
Swift 4 supported from 1.2.0 version.
Install v1.0.0 if you need to support Swift 2.3.
Install v1.0.2 and above if you want to support Swift 3.
Demo
Check out the Example project.
Author
Alejandro Pasccon, apasccon@gmail.com
License
SearchTextField is available under the MIT license. See the LICENSE file for more info.