Commit 8c653e91 by Daniel Dahan

development: Added SearchBar delegation methods for when text changes and for…

development: Added SearchBar delegation methods for when text changes and for when the text has been cleared.
parent a89ba9a7
...@@ -32,6 +32,13 @@ import UIKit ...@@ -32,6 +32,13 @@ import UIKit
extension String { extension String {
/** /**
:name: trim
*/
public var trimmed: String {
return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
/**
:name: lines :name: lines
*/ */
public var lines: [String] { public var lines: [String] {
...@@ -42,34 +49,27 @@ extension String { ...@@ -42,34 +49,27 @@ extension String {
:name: firstLine :name: firstLine
*/ */
public var firstLine: String? { public var firstLine: String? {
return lines.first?.trim() return lines.first?.trimmed
} }
/** /**
:name: lastLine :name: lastLine
*/ */
public var lastLine: String? { public var lastLine: String? {
return lines.last?.trim() return lines.last?.trimmed
} }
/** /**
:name: replaceNewLineCharater :name: replaceNewLineCharater
*/ */
public func replaceNewLineCharater(separator: String = " ") -> String { public func replaceNewLineCharater(separator: String = " ") -> String {
return components(separatedBy: CharacterSet.whitespaces).joined(separator: separator).trim() return components(separatedBy: CharacterSet.whitespaces).joined(separator: separator).trimmed
} }
/** /**
:name: replacePunctuationCharacters :name: replacePunctuationCharacters
*/ */
public func replacePunctuationCharacters(separator: String = "") -> String { public func replacePunctuationCharacters(separator: String = "") -> String {
return components(separatedBy: CharacterSet.punctuationCharacters).joined(separator: separator).trim() return components(separatedBy: CharacterSet.punctuationCharacters).joined(separator: separator).trimmed
}
/**
:name: trim
*/
public func trim() -> String {
return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
} }
} }
...@@ -30,6 +30,27 @@ ...@@ -30,6 +30,27 @@
import UIKit import UIKit
@objc(SearchBarDelegate)
public protocol SearchBarDelegate {
/**
A delegation method that is executed when the textField changed.
- Parameter searchBar: A SearchBar.
- Parameter didChange textField: A UITextField.
- Parameter with text: An optional String.
*/
@objc
optional func searchBar(searchBar: SearchBar, didChange textField: UITextField, with text: String?)
/**
A delegation method that is executed when the textField is cleared.
- Parameter searchBar: A SearchBar.
- Parameter didClear textField: A UITextField.
- Parameter with text: An optional String.
*/
@objc
optional func searchBar(searchBar: SearchBar, didClear textField: UITextField, with text: String?)
}
open class SearchBar: Bar { open class SearchBar: Bar {
/// The UITextField for the searchBar. /// The UITextField for the searchBar.
open private(set) lazy var textField = UITextField() open private(set) lazy var textField = UITextField()
...@@ -37,6 +58,9 @@ open class SearchBar: Bar { ...@@ -37,6 +58,9 @@ open class SearchBar: Bar {
/// Reference to the clearButton. /// Reference to the clearButton.
open private(set) var clearButton: IconButton! open private(set) var clearButton: IconButton!
/// A reference to the delegate.
open weak var delegate: SearchBarDelegate?
/// Handle the clearButton manually. /// Handle the clearButton manually.
@IBInspectable @IBInspectable
open var isClearButtonAutoHandleEnabled = true { open var isClearButtonAutoHandleEnabled = true {
...@@ -151,9 +175,16 @@ open class SearchBar: Bar { ...@@ -151,9 +175,16 @@ open class SearchBar: Bar {
/// Clears the textField text. /// Clears the textField text.
@objc @objc
internal func handleClearButton() { internal func handleClearButton() {
delegate?.searchBar?(searchBar: self, didClear: textField, with: textField.text)
textField.text = nil textField.text = nil
} }
// Live updates the search results.
@objc
internal func handleTextChange(textField: UITextField) {
delegate?.searchBar?(searchBar: self, didChange: textField, with: textField.text)
}
/// Prepares the textField. /// Prepares the textField.
private func prepareTextField() { private func prepareTextField() {
textField.contentScaleFactor = Device.scale textField.contentScaleFactor = Device.scale
...@@ -164,6 +195,7 @@ open class SearchBar: Bar { ...@@ -164,6 +195,7 @@ open class SearchBar: Bar {
textColor = Color.darkText.primary textColor = Color.darkText.primary
placeholder = "Search" placeholder = "Search"
contentView.addSubview(textField) contentView.addSubview(textField)
textField.addTarget(self, action: #selector(handleTextChange(textField:)), for: .editingChanged)
} }
/// Prepares the clearButton. /// Prepares the clearButton.
......
...@@ -97,8 +97,8 @@ public class Text: NSObject { ...@@ -97,8 +97,8 @@ public class Text: NSObject {
/// An Array of matches that match the pattern expression. /// An Array of matches that match the pattern expression.
public var matches: Array<String> { public var matches: Array<String> {
return textStorage.expression!.matches(in: string, options: [], range: NSMakeRange(0, string.utf16.count)).map { return textStorage.expression!.matches(in: string, options: [], range: NSMakeRange(0, string.utf16.count)).map { [unowned self] in
(self.string as NSString).substring(with: $0.range).trim() (self.string as NSString).substring(with: $0.range).trimmed
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment