Easy to use and highly configurable autocomplete view that is attachable to any UITextField
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ sudo gem install cocoapods
CocoaPods 1.6.0+ is required.
To integrate LUAutocompleteView
into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'LUAutocompleteView'
end
Then, run the following command:
$ pod install
You can use Carthage to install LUAutocompleteView
by adding it to your Cartfile
:
github "LaurentiuUngur/LUAutocompleteView" ~> 3.0
Then run carthage update
.
If this is your first time using Carthage in the project, you'll need to go through some additional steps as explained over at Carthage.
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
:
.Package(url: "https://github.com/LaurentiuUngur/LUAutocompleteView", majorVersion: 3)
Here's an example of PackageDescription
:
import PackageDescription
let package = Package(name: "MyApp",
dependencies: [
.Package(url: "https://github.com/LaurentiuUngur/LUAutocompleteView", majorVersion: 3)
])
If you prefer not to use either of the before mentioned dependency managers, you can integrate LUAutocompleteView
into your project manually.
- Import
LUAutocompleteView
into your project.
import LUAutocompleteView
- Assign to
textField
property the text field to which the autocomplete view you want be attached.
autocompleteView.textField = textField
- Set as data source and delegate.
autocompleteView.dataSource = self
autocompleteView.delegate = self
- Implement
LUAutocompleteViewDataSource
andLUAutocompleteViewDelegate
protocols.
// MARK: - LUAutocompleteViewDataSource
extension ViewController: LUAutocompleteViewDataSource {
func autocompleteView(_ autocompleteView: LUAutocompleteView, elementsFor text: String, completion: @escaping ([String]) -> Void) {
let elementsThatMatchInput = elements.filter { $0.lowercased().contains(text.lowercased()) }
completion(elementsThatMatchInput)
}
}
// MARK: - LUAutocompleteViewDelegate
extension ViewController: LUAutocompleteViewDelegate {
func autocompleteView(_ autocompleteView: LUAutocompleteView, didSelect text: String) {
print(text + " was selected from autocomplete view")
}
}
- Create your custom autocomplete cell by subclassing
LUAutocompleteTableViewCell
. - Override
func set(text: String)
fromLUAutocompleteTableViewCell
that is called every time when given text should be displayed by the cell.
import UIKit
import LUAutocompleteView
final class CustomAutocompleteTableViewCell: LUAutocompleteTableViewCell {
// MARK: - Base Class Overrides
override func set(text: String) {
textLabel?.text = text
textLabel?.textColor = .red
}
}
- Assign to
autocompleteCell
property your custom autocomplete cell.
autocompleteView.autocompleteCell = CustomAutocompleteTableViewCell.self
- Xcode 10.0+
- Swift 4.2+
- iOS 9.0+
- LUAutocompleteView is available under the MIT license.