Skip to content

Commit

Permalink
Add text field text changed handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhquan committed Jan 14, 2022
1 parent 77bf91b commit 87f058a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 38 deletions.
50 changes: 40 additions & 10 deletions Source/SwiftAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ public class SwiftAlertView: UIView {
public var backgroundImage: UIImage?
// public var backgroundColor: UIColor? // inherits from UIView

public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button
public var cancelButtonIndex = 0 { // default is 0, set this property if you want to change the position of cancel button
didSet {
for i in 0..<buttons.count {
let button = buttons[i]
if i == cancelButtonIndex {
button.titleLabel?.font = .boldSystemFont(ofSize: button.titleLabel?.font.pointSize ?? 17)
} else {
button.titleLabel?.font = .systemFont(ofSize: button.titleLabel?.font.pointSize ?? 17)
}
}
}
}
public var buttonTitleColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1) // to change the title color of all buttons
public var buttonHeight: CGFloat = 44.0

Expand Down Expand Up @@ -97,12 +108,7 @@ public class SwiftAlertView: UIView {
public var validationLabelTopMargin: CGFloat = 8.0
public var validationLabelSideMargin: CGFloat = 15.0

// closures for handling button clicked action
public var onButtonClicked: ((_ buttonIndex: Int) -> Void)? // all buttons
public var onCancelClicked: (() -> Void)? // for cancel button
public var onActionButtonClicked: ((_ buttonIndex: Int) -> (Void))? // sometimes you want to handle the action button clicked event but don't want to write if/else in onButtonClicked closure, use this property



// MARK: Constants

private let kSeparatorWidth: CGFloat = 0.5
Expand Down Expand Up @@ -133,6 +139,11 @@ public class SwiftAlertView: UIView {
private var viewWidth: CGFloat = 0
private var viewHeight: CGFloat = 0
private var isMoveUpWithKeyboard = false

private var onButtonClicked: ((_ buttonIndex: Int) -> Void)?
private var onCancelClicked: (() -> Void)?
private var onActionButtonClicked: ((_ buttonIndex: Int) -> (Void))?
private var onTextChanged: ((_ text: String?, _ textFieldIndex: Int) -> Void)?

// MARK: Initialization

Expand Down Expand Up @@ -202,6 +213,8 @@ public class SwiftAlertView: UIView {
textField.font = .systemFont(ofSize: 14)
textField.borderStyle = .roundedRect
textField.delegate = self
textField.tag = textFields.count
textField.addTarget(self, action: #selector(textChanged(_:)), for: .editingChanged)
configurationHandler?(textField)
textFields.append(textField)
addSubview(textField)
Expand Down Expand Up @@ -337,17 +350,29 @@ public class SwiftAlertView: UIView {
}
}

// handle button click events
public func onButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) {
// handle events
@discardableResult
public func onButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView {
self.onButtonClicked = { index in
handler(self, index)
}
return self
}

public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) {
@discardableResult
public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView {
self.onActionButtonClicked = { index in
handler(self, index)
}
return self
}

@discardableResult
public func onTextChanged(_ handler: @escaping (_ alertView: SwiftAlertView, _ text: String?, _ textFieldIndex: Int) -> Void) -> SwiftAlertView {
self.onTextChanged = { text, index in
handler(self, text, index)
}
return self
}
}

Expand Down Expand Up @@ -613,6 +638,11 @@ extension SwiftAlertView {
}
}

@objc private func textChanged(_ textField: UITextField) {
let index = textField.tag
onTextChanged?(textField.text, index)
}


// MARK: Utils

Expand Down
61 changes: 33 additions & 28 deletions SwiftAlertView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import UIKit
final class ViewController: UITableViewController {

let demoTitles: [String] = ["Dark Mode",
"More Than Two Buttons",
"Add Text Fields",
"Alert with Text Field",
"More Text Fields & Validation Label",
"Customize Font & Color",
"Custom Content View",
"Init From Nib File",
Expand All @@ -30,33 +30,30 @@ final class ViewController: UITableViewController {

switch indexPath.row {
case 0:
let alertView = SwiftAlertView(title: "Lorem ipsum",
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
buttonTitles: "Cancel", "Ok")
alertView.style = .dark
alertView.onButtonClicked = { buttonIndex in
SwiftAlertView.show(title: "Lorem ipsum",
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
buttonTitles: "Cancel", "Ok") {
$0.style = .dark
}
.onButtonClicked { _, buttonIndex in
print("Button Clicked At Index \(buttonIndex)")
}

alertView.onCancelClicked = {
print("Cancel Button Clicked")

case 1:
SwiftAlertView.show(title: "Title",
message: "Message",
buttonTitles: "Button 1", "Button 2", "Button 3", "Cancel") { alertView in
alertView.style = .auto
alertView.cancelButtonIndex = 3
alertView.buttonTitleColor = UIColor(red: 0.8764, green: 0.5, blue: 0.3352, alpha: 1)
alertView.addTextField { textField in
textField.placeholder = "Placeholder"
}
}

alertView.onActionButtonClicked = { _ in
print("Action Button Clicked")
.onActionButtonClicked { alert, buttonIndex in
let text = alert.textField(at: 0)?.text ?? ""
print("Text: ", text)
}

alertView.show()

case 1:
let alertView = SwiftAlertView(title: "Lorem ipsum",
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
buttonTitles: "Button 1", "Button 2", "Button 3", "Cancel")
alertView.style = .auto
alertView.delegate = self
alertView.cancelButtonIndex = 3
alertView.buttonTitleColor = UIColor(red: 0.8764, green: 0.5, blue: 0.3352, alpha: 1)
alertView.show()

case 2:
SwiftAlertView.show(title: "Sign in", buttonTitles: "Cancel", "Sign In") { alertView in
Expand All @@ -71,8 +68,16 @@ final class ViewController: UITableViewController {
}
.onActionButtonClicked { alert, buttonIndex in
let username = alert.textField(at: 0)?.text ?? ""
print("Username: ", username)
alert.validationLabel.text = "Username is incorrect"
if username.isEmpty {
alert.validationLabel.text = "Username is incorrect"
} else {
alert.dismiss()
}
}
.onTextChanged { _, text, index in
if index == 0 {
print("Username text changed: ", text ?? "")
}
}

case 3:
Expand All @@ -89,7 +94,7 @@ final class ViewController: UITableViewController {
alertView.button(at: 1)?.setTitleColor(.purple, for: .normal)
alertView.button(at: 0)?.titleLabel?.font = UIFont(name: "Marker Felt", size: 20)
alertView.button(at: 1)?.titleLabel?.font = UIFont(name: "Marker Felt", size: 20)

alertView.delegate = self
alertView.show()

case 4:
Expand Down

0 comments on commit 87f058a

Please sign in to comment.