From d0d86a63c5407fe88db09b1d51db213be60461ce Mon Sep 17 00:00:00 2001 From: Scott Delly Date: Thu, 7 Sep 2017 18:54:04 -0700 Subject: [PATCH 1/2] Added the ability to autocomplete the contents of the text field on tab instead of return --- SearchTextField/Classes/SearchTextField.swift | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/SearchTextField/Classes/SearchTextField.swift b/SearchTextField/Classes/SearchTextField.swift index 3ead673..798861f 100755 --- a/SearchTextField/Classes/SearchTextField.swift +++ b/SearchTextField/Classes/SearchTextField.swift @@ -117,6 +117,14 @@ open class SearchTextField: UITextField { /// Set the results list's header open var resultsListHeader: UIView? + /// If set to true, when editing ends with tab the top result will be used as the final field text. Otherwise autocomplete happens on return + open var autocompleteOnTab: Bool = false + + /// Get the current filter items that are displayed to the user + open func currentFilterItems() -> [SearchTextFieldItem] { + return self.filteredResults + } + //////////////////////////////////////////////////////////////////////// // Private implementation @@ -130,6 +138,7 @@ open class SearchTextField: UITextField { fileprivate static let cellIdentifier = "APSearchTextFieldCell" fileprivate let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) fileprivate var maxTableViewSize: CGFloat = 0 + fileprivate var returnKeyPressed: Bool = false fileprivate var filteredResults = [SearchTextFieldItem]() fileprivate var filterDataSource = [SearchTextFieldItem]() { @@ -377,12 +386,23 @@ open class SearchTextField: UITextField { } open func textFieldDidEndEditing() { + if autocompleteOnTab && !returnKeyPressed { + performAutocomplete() + } clearResults() tableView?.reloadData() placeholderLabel?.attributedText = nil + returnKeyPressed = false } open func textFieldDidEndEditingOnExit() { + returnKeyPressed = true + if !autocompleteOnTab { + performAutocomplete() + } + } + + func performAutocomplete() { if let firstElement = filteredResults.first { if let itemSelectionHandler = self.itemSelectionHandler { itemSelectionHandler(filteredResults, 0) From 2838c213a5621e12b13a91acbabbacdc34ec511a Mon Sep 17 00:00:00 2001 From: Scott Delly Date: Wed, 20 Sep 2017 15:14:28 -0700 Subject: [PATCH 2/2] Added var to adjust the filter table display animation duration --- SearchTextField/Classes/SearchTextField.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/SearchTextField/Classes/SearchTextField.swift b/SearchTextField/Classes/SearchTextField.swift index 798861f..b045f48 100755 --- a/SearchTextField/Classes/SearchTextField.swift +++ b/SearchTextField/Classes/SearchTextField.swift @@ -72,7 +72,7 @@ open class SearchTextField: UITextField { open var itemSelectionHandler: SearchTextFieldItemHandler? /// Closure to handle when the user stops typing - open var userStoppedTypingHandler: ((Void) -> Void)? + open var userStoppedTypingHandler: (() -> ())? /// Set your custom set of attributes in order to highlight the string found in each item open var highlightAttributes: [String: AnyObject] = [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 10)] @@ -120,6 +120,8 @@ open class SearchTextField: UITextField { /// If set to true, when editing ends with tab the top result will be used as the final field text. Otherwise autocomplete happens on return open var autocompleteOnTab: Bool = false + open var filterListDisplayAnimationDuration: TimeInterval = 0.2 + /// Get the current filter items that are displayed to the user open func currentFilterItems() -> [SearchTextFieldItem] { return self.filteredResults @@ -286,7 +288,7 @@ open class SearchTextField: UITextField { tableViewFrame.origin = self.convert(tableViewFrame.origin, to: nil) tableViewFrame.origin.x += 2 tableViewFrame.origin.y += frame.size.height + 2 - UIView.animate(withDuration: 0.2, animations: { [weak self] in + UIView.animate(withDuration: filterListDisplayAnimationDuration, animations: { [weak self] in self?.tableView?.frame = tableViewFrame }) @@ -297,7 +299,7 @@ open class SearchTextField: UITextField { shadowView!.frame = shadowFrame } else { let tableHeight = min((tableView.contentSize.height), (UIScreen.main.bounds.size.height - frame.origin.y - theme.cellHeight)) - UIView.animate(withDuration: 0.2, animations: { [weak self] in + UIView.animate(withDuration: filterListDisplayAnimationDuration, animations: { [weak self] in self?.tableView?.frame = CGRect(x: frame.origin.x + 2, y: (frame.origin.y - tableHeight), width: frame.size.width - 4, height: tableHeight) self?.shadowView?.frame = CGRect(x: frame.origin.x + 3, y: (frame.origin.y + 3), width: frame.size.width - 6, height: 1) }) @@ -422,7 +424,7 @@ open class SearchTextField: UITextField { open func hideResultsList() { if let tableFrame:CGRect = tableView?.frame { let newFrame = CGRect(x: tableFrame.origin.x, y: tableFrame.origin.y, width: tableFrame.size.width, height: 0.0) - UIView.animate(withDuration: 0.2, animations: { [weak self] in + UIView.animate(withDuration: filterListDisplayAnimationDuration, animations: { [weak self] in self?.tableView?.frame = newFrame })