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
......@@ -31,7 +31,14 @@
import UIKit
extension String {
/**
/**
:name: trim
*/
public var trimmed: String {
return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
/**
:name: lines
*/
public var lines: [String] {
......@@ -42,34 +49,27 @@ extension String {
:name: firstLine
*/
public var firstLine: String? {
return lines.first?.trim()
return lines.first?.trimmed
}
/**
:name: lastLine
*/
public var lastLine: String? {
return lines.last?.trim()
return lines.last?.trimmed
}
/**
:name: replaceNewLineCharater
*/
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
*/
public func replacePunctuationCharacters(separator: String = "") -> String {
return components(separatedBy: CharacterSet.punctuationCharacters).joined(separator: separator).trim()
}
/**
:name: trim
*/
public func trim() -> String {
return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return components(separatedBy: CharacterSet.punctuationCharacters).joined(separator: separator).trimmed
}
}
......@@ -30,6 +30,27 @@
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 {
/// The UITextField for the searchBar.
open private(set) lazy var textField = UITextField()
......@@ -37,6 +58,9 @@ open class SearchBar: Bar {
/// Reference to the clearButton.
open private(set) var clearButton: IconButton!
/// A reference to the delegate.
open weak var delegate: SearchBarDelegate?
/// Handle the clearButton manually.
@IBInspectable
open var isClearButtonAutoHandleEnabled = true {
......@@ -151,9 +175,16 @@ open class SearchBar: Bar {
/// Clears the textField text.
@objc
internal func handleClearButton() {
textField.text = nil
}
delegate?.searchBar?(searchBar: self, didClear: textField, with: textField.text)
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.
private func prepareTextField() {
textField.contentScaleFactor = Device.scale
......@@ -164,6 +195,7 @@ open class SearchBar: Bar {
textColor = Color.darkText.primary
placeholder = "Search"
contentView.addSubview(textField)
textField.addTarget(self, action: #selector(handleTextChange(textField:)), for: .editingChanged)
}
/// Prepares the clearButton.
......
......@@ -97,8 +97,8 @@ public class Text: NSObject {
/// An Array of matches that match the pattern expression.
public var matches: Array<String> {
return textStorage.expression!.matches(in: string, options: [], range: NSMakeRange(0, string.utf16.count)).map {
(self.string as NSString).substring(with: $0.range).trim()
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).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