Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/table-corner-radius #62

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions Example/SearchTextField/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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()
}
Expand Down Expand Up @@ -60,14 +60,17 @@ 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)
acronymTextField.theme.borderColor = UIColor.lightGray.withAlphaComponent(0.5)
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
Expand All @@ -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)]

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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<UITouch>, with event: UIEvent?) {
view.endEditing(true)
Expand Down Expand Up @@ -208,6 +214,6 @@ class MainViewController: UITableViewController {
task.resume()
}
}


}
59 changes: 37 additions & 22 deletions SearchTextField/Classes/SearchTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
})
Expand All @@ -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)
})
}
Expand All @@ -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

Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -646,7 +660,8 @@ public typealias SearchTextFieldItemHandler = (_ filteredResults: [SearchTextFie
////////////////////////////////////////////////////////////////////////
// Suggestions List Direction

enum Direction {
public enum Direction {
case down
case up
case auto//Default
}