Skip to content

Commit

Permalink
Merge pull request #101 from alexbrie/swift4-and-enhancements
Browse files Browse the repository at this point in the history
Extended theming (and Swift 4 compatibility)
  • Loading branch information
apasccon authored Jan 22, 2018
2 parents 327458f + 194267b commit fd4f73e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions SearchTextField/Classes/SearchTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ open class SearchTextField: UITextField {
open var userStoppedTypingHandler: (() -> Void)?

/// Set your custom set of attributes in order to highlight the string found in each item
open var highlightAttributes: [NSAttributedStringKey: AnyObject] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont.boldSystemFont(ofSize: 10)]
open var highlightAttributes: [NSAttributedStringKey: AnyObject] = [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 10)]

/// Start showing the default loading indicator, useful for searches that take some time.
open func showLoadingIndicator() {
Expand Down Expand Up @@ -107,6 +107,9 @@ open class SearchTextField: UITextField {

/// Min number of characters to start filtering
open var minCharactersNumberToStartFiltering: Int = 0

/// Force no filtering (display the entire filtered data source)
open var forceNoFiltering: Bool = false

/// If startFilteringAfter is set, and startSuggestingInmediately is true, the list of suggestions appear inmediately
open var startSuggestingInmediately = false
Expand Down Expand Up @@ -134,7 +137,7 @@ open class SearchTextField: UITextField {
fileprivate var filteredResults = [SearchTextFieldItem]()
fileprivate var filterDataSource = [SearchTextFieldItem]() {
didSet {
filter(forceShowAll: false)
filter(forceShowAll: forceNoFiltering)
buildSearchTableView()

if startVisibleWithoutInteraction {
Expand Down Expand Up @@ -191,7 +194,7 @@ open class SearchTextField: UITextField {
fileprivate func buildSearchTableView() {
if let tableView = tableView, let shadowView = shadowView {
tableView.layer.masksToBounds = true
tableView.layer.borderWidth = 0.5
tableView.layer.borderWidth = theme.borderWidth > 0 ? theme.borderWidth : 0.5
tableView.dataSource = self
tableView.delegate = self
tableView.separatorInset = UIEdgeInsets.zero
Expand Down Expand Up @@ -302,6 +305,7 @@ open class SearchTextField: UITextField {
}

tableView.layer.borderColor = theme.borderColor.cgColor
tableView.layer.borderWidth = 1
tableView.layer.cornerRadius = 2
tableView.separatorColor = theme.separatorColor
tableView.backgroundColor = theme.bgColor
Expand Down Expand Up @@ -361,7 +365,7 @@ open class SearchTextField: UITextField {
}
self.placeholderLabel?.text = ""
} else {
filter(forceShowAll: false)
filter(forceShowAll: forceNoFiltering)
prepareDrawTableResult()
}

Expand Down Expand Up @@ -554,7 +558,7 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource {
cell!.textLabel?.font = theme.font
cell!.detailTextLabel?.font = UIFont(name: theme.font.fontName, size: theme.font.pointSize * fontConversionRate)
cell!.textLabel?.textColor = theme.fontColor
cell!.detailTextLabel?.textColor = theme.fontColor
cell!.detailTextLabel?.textColor = theme.subtitleFontColor

cell!.textLabel?.text = filteredResults[(indexPath as NSIndexPath).row].title
cell!.detailTextLabel?.text = filteredResults[(indexPath as NSIndexPath).row].subtitle
Expand Down Expand Up @@ -591,18 +595,21 @@ public struct SearchTextFieldTheme {
public var cellHeight: CGFloat
public var bgColor: UIColor
public var borderColor: UIColor
public var borderWidth : CGFloat = 0
public var separatorColor: UIColor
public var font: UIFont
public var fontColor: UIColor
public var subtitleFontColor: UIColor
public var placeholderColor: UIColor?

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, subtitleFontColor: UIColor? = nil) {
self.cellHeight = cellHeight
self.borderColor = borderColor
self.separatorColor = separatorColor
self.bgColor = bgColor
self.font = font
self.fontColor = fontColor
self.subtitleFontColor = subtitleFontColor ?? fontColor
}

public static func lightTheme() -> SearchTextFieldTheme {
Expand Down

0 comments on commit fd4f73e

Please sign in to comment.