diff --git a/Example/SearchTextField/MainViewController.swift b/Example/SearchTextField/MainViewController.swift index 73e1cea..b2466a8 100644 --- a/Example/SearchTextField/MainViewController.swift +++ b/Example/SearchTextField/MainViewController.swift @@ -10,7 +10,7 @@ import UIKit import SearchTextField class MainViewController: UITableViewController { - + @IBOutlet weak var countryTextField: SearchTextField! @IBOutlet weak var acronymTextField: SearchTextField! @IBOutlet weak var countryInLineTextField: SearchTextField! @@ -28,10 +28,10 @@ class MainViewController: UITableViewController { // 2 - Configure a custom search text field configureCustomSearchTextField() - + // 3 - Configure an "inline" suggestions search text field configureSimpleInLineSearchTextField() - + // 4 - Configure a custom "inline" suggestions search text field configureCustomInLineSearchTextField() } @@ -60,7 +60,6 @@ class MainViewController: UITableViewController { header.text = "Pick your option" acronymTextField.resultsListHeader = header - // Modify current theme properties acronymTextField.theme.font = UIFont.systemFont(ofSize: 12) acronymTextField.theme.bgColor = UIColor.lightGray.withAlphaComponent(0.2) @@ -68,6 +67,10 @@ class MainViewController: UITableViewController { acronymTextField.theme.separatorColor = UIColor.lightGray.withAlphaComponent(0.5) acronymTextField.theme.cellHeight = 50 acronymTextField.theme.placeholderColor = UIColor.lightGray + //Adds padding to the table view. This leaves a visible gap between the text field and search field. + acronymTextField.theme.padding = 10 + //Add corner radius to search results table view + acronymTextField.theme.cornerRadius = 5 // Max number of results - Default: No limit acronymTextField.maxNumberOfResults = 5 @@ -77,10 +80,10 @@ class MainViewController: UITableViewController { // Set specific comparision options - Default: .caseInsensitive acronymTextField.comparisonOptions = [.caseInsensitive] - + // You can force the results list to support RTL languages - Default: false acronymTextField.forceRightToLeft = false - + // Customize highlight attributes - Default: Bold acronymTextField.highlightAttributes = [NSBackgroundColorAttributeName: UIColor.yellow, NSFontAttributeName:UIFont.boldSystemFont(ofSize: 12)] @@ -94,6 +97,9 @@ class MainViewController: UITableViewController { self.acronymTextField.text = item.title } + // You can force the results table appear always top + acronymTextField.direction = .up + // Update data source when the user stops typing acronymTextField.userStoppedTypingHandler = { if let criteria = self.acronymTextField.text { @@ -124,7 +130,7 @@ class MainViewController: UITableViewController { let countries = localCountries() countryInLineTextField.filterStrings(countries) } - + // 4 - Configure a custom inline search text view fileprivate func configureCustomInLineSearchTextField() { // Define the inline mode @@ -136,7 +142,7 @@ class MainViewController: UITableViewController { // Set data source emailInlineTextField.filterStrings(["gmail.com", "yahoo.com", "yahoo.com.ar"]) } - + // Hide keyboard when touching the screen override func touchesEnded(_ touches: Set, with event: UIEvent?) { view.endEditing(true) @@ -208,6 +214,6 @@ class MainViewController: UITableViewController { task.resume() } } - - + + } diff --git a/SearchTextField/Classes/SearchTextField.swift b/SearchTextField/Classes/SearchTextField.swift index e3daeb6..6d89420 100755 --- a/SearchTextField/Classes/SearchTextField.swift +++ b/SearchTextField/Classes/SearchTextField.swift @@ -116,13 +116,14 @@ open class SearchTextField: UITextField { /// Set the results list's header open var resultsListHeader: UIView? - + + /// Set the direction of the results table. If nothing sets default is automatic + open var direction: Direction = .auto //////////////////////////////////////////////////////////////////////// // Private implementation fileprivate var tableView: UITableView? fileprivate var shadowView: UIView? - fileprivate var direction: Direction = .down fileprivate var fontConversionRate: CGFloat = 0.7 fileprivate var keyboardFrame: CGRect? fileprivate var timer: Timer? = nil @@ -254,9 +255,7 @@ open class SearchTextField: UITextField { if let tableView = tableView { guard let frame = self.superview?.convert(self.frame, to: nil) else { return } - - if self.direction == .down { - + if self.direction == .down || self.direction == .auto { var tableHeight: CGFloat = 0 if keyboardIsShowing, let keyboardHeight = keyboardFrame?.size.height { tableHeight = min((tableView.contentSize.height), (UIScreen.main.bounds.size.height - frame.origin.y - frame.height - keyboardHeight)) @@ -276,7 +275,7 @@ open class SearchTextField: UITextField { var tableViewFrame = CGRect(x: 0, y: 0, width: frame.size.width - 4, height: tableHeight) tableViewFrame.origin = self.convert(tableViewFrame.origin, to: nil) tableViewFrame.origin.x += 2 - tableViewFrame.origin.y += frame.size.height + 2 + tableViewFrame.origin.y += frame.size.height + 2 + theme.padding UIView.animate(withDuration: 0.2, animations: { [weak self] in self?.tableView?.frame = tableViewFrame }) @@ -289,7 +288,7 @@ open class SearchTextField: UITextField { } 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 - self?.tableView?.frame = CGRect(x: frame.origin.x + 2, y: (frame.origin.y - tableHeight), width: frame.size.width - 4, height: tableHeight) + self?.tableView?.frame = CGRect(x: frame.origin.x + 2, y: (frame.origin.y - tableHeight), width: frame.size.width - 4, height: tableHeight - (self?.theme.padding)!) self?.shadowView?.frame = CGRect(x: frame.origin.x + 3, y: (frame.origin.y + 3), width: frame.size.width - 6, height: 1) }) } @@ -302,7 +301,7 @@ open class SearchTextField: UITextField { } tableView.layer.borderColor = theme.borderColor.cgColor - tableView.layer.cornerRadius = 2 + tableView.layer.cornerRadius = theme.cornerRadius tableView.separatorColor = theme.separatorColor tableView.backgroundColor = theme.bgColor @@ -323,7 +322,9 @@ open class SearchTextField: UITextField { open func keyboardWillHide(_ notification: Notification) { if keyboardIsShowing { keyboardIsShowing = false - direction = .down + if direction == .auto { + direction = .down + } redrawSearchTableView() } } @@ -505,17 +506,26 @@ open class SearchTextField: UITextField { // MARK: - Prepare for draw table result fileprivate func prepareDrawTableResult() { - guard let frame = self.superview?.convert(self.frame, to: UIApplication.shared.keyWindow) else { return } - if let keyboardFrame = keyboardFrame { - var newFrame = frame - newFrame.size.height += theme.cellHeight - - if keyboardFrame.intersects(newFrame) { - direction = .up + if direction == .auto {//if direction is not auto ignore and force the layout to draw AS IS required. + guard let frame = self.superview?.convert(self.frame, to: UIApplication.shared.keyWindow) else { return } + if let keyboardFrame = keyboardFrame { + var newFrame = frame + newFrame.size.height += theme.cellHeight + + if keyboardFrame.intersects(newFrame) { + direction = .up + } else { + direction = .down + } + + redrawSearchTableView() } else { - direction = .down + if self.center.y + theme.cellHeight > UIApplication.shared.keyWindow!.frame.size.height { + direction = .up + } else { + direction = .down + } } - redrawSearchTableView() } else { if self.center.y + theme.cellHeight > UIApplication.shared.keyWindow!.frame.size.height { @@ -593,22 +603,26 @@ public struct SearchTextFieldTheme { public var font: UIFont public var fontColor: UIColor public var placeholderColor: UIColor? + public var padding: CGFloat + public var cornerRadius: CGFloat - init(cellHeight: CGFloat, bgColor:UIColor, borderColor: UIColor, separatorColor: UIColor, font: UIFont, fontColor: UIColor) { + init(cellHeight: CGFloat, bgColor:UIColor, borderColor: UIColor, separatorColor: UIColor, font: UIFont, fontColor: UIColor, padding: CGFloat, cornerRadius: CGFloat) { self.cellHeight = cellHeight self.borderColor = borderColor self.separatorColor = separatorColor self.bgColor = bgColor self.font = font self.fontColor = fontColor + self.padding = padding + self.cornerRadius = cornerRadius } public static func lightTheme() -> SearchTextFieldTheme { - return SearchTextFieldTheme(cellHeight: 30, bgColor: UIColor (red: 1, green: 1, blue: 1, alpha: 0.6), borderColor: UIColor (red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0), separatorColor: UIColor.clear, font: UIFont.systemFont(ofSize: 10), fontColor: UIColor.black) + return SearchTextFieldTheme(cellHeight: 30, bgColor: UIColor (red: 1, green: 1, blue: 1, alpha: 0.6), borderColor: UIColor (red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0), separatorColor: UIColor.clear, font: UIFont.systemFont(ofSize: 10), fontColor: UIColor.black, padding: 0.0, cornerRadius: 2) } public static func darkTheme() -> SearchTextFieldTheme { - return SearchTextFieldTheme(cellHeight: 30, bgColor: UIColor (red: 0.8, green: 0.8, blue: 0.8, alpha: 0.6), borderColor: UIColor (red: 0.7, green: 0.7, blue: 0.7, alpha: 1.0), separatorColor: UIColor.clear, font: UIFont.systemFont(ofSize: 10), fontColor: UIColor.white) + return SearchTextFieldTheme(cellHeight: 30, bgColor: UIColor (red: 0.8, green: 0.8, blue: 0.8, alpha: 0.6), borderColor: UIColor (red: 0.7, green: 0.7, blue: 0.7, alpha: 1.0), separatorColor: UIColor.clear, font: UIFont.systemFont(ofSize: 10), fontColor: UIColor.white, padding: 0.0, cornerRadius: 2) } } @@ -646,7 +660,8 @@ public typealias SearchTextFieldItemHandler = (_ filteredResults: [SearchTextFie //////////////////////////////////////////////////////////////////////// // Suggestions List Direction -enum Direction { +public enum Direction { case down case up + case auto//Default }