Skip to content

Commit f862462

Browse files
committed
Merge pull request #20 from bcylin/feature/did-change-input-text
Close #15
2 parents 6c002a1 + 2ea8817 commit f862462

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

Example/CustomizedTokenField.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class CustomizedTokenField: ICTokenField {
3939
applyCustomizedStyle()
4040
}
4141

42+
override var intrinsicContentSize: CGSize {
43+
return UILayoutFittingExpandedSize
44+
}
45+
4246
}
4347

4448

Example/CustomizedTokenViewController.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,20 @@ class CustomizedTokenViewController: UIViewController, ICTokenFieldDelegate {
5656
cancelBarButton.tintColor = UIColor.white
5757
navigationItem.rightBarButtonItem = cancelBarButton
5858

59-
navigationItem.titleView = tokenField
59+
navigationItem.titleView = {
60+
if #available(iOS 11, *) {
61+
tokenField.translatesAutoresizingMaskIntoConstraints = false
62+
let views = ["field": tokenField]
63+
let metrics = ["padding": 7]
64+
let containerView = UIView()
65+
containerView.addSubview(tokenField)
66+
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[field]-padding-|", options: [], metrics: metrics, views: views))
67+
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-padding-[field]-padding-|", options: [], metrics: metrics, views: views))
68+
return containerView
69+
} else {
70+
return tokenField
71+
}
72+
}()
6073
tokenField.delegate = self
6174
}
6275

Example/ExampleViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ExampleViewController: UITableViewController {
5454

5555
override func loadView() {
5656
super.loadView()
57+
tableView.rowHeight = 44
5758
tableView.register(ExampleCell.self, forCellReuseIdentifier: String(describing: ExampleCell.self))
5859
tableView.tableFooterView = flipButton
5960
tableView.tableFooterView?.isUserInteractionEnabled = true

Source/TokenField/ICTokenField.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,25 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
328328
// MARK: - ICBackspaceTextFieldDelegate
329329

330330
@nonobjc func textFieldShouldDelete(_ textField: ICBackspaceTextField) -> Bool {
331-
if tokens.isEmpty {
331+
if !textField.showsCursor {
332+
_ = removeHighlightedToken()
332333
return true
333334
}
334335

335-
if !textField.showsCursor {
336-
_ = removeHighlightedToken()
336+
guard let text = textField.text else {
337337
return true
338338
}
339339

340-
if let text = textField.text, text.isEmpty {
340+
if text.isEmpty {
341341
textField.showsCursor = false
342342
tokens.last?.isHighlighted = true
343+
} else {
344+
// textField(_:shouldChangeCharactersIn:replacementString:) is skipped when the delete key is pressed.
345+
// Notify the delegate of the changed input text manually.
346+
let index = text.index(text.endIndex, offsetBy: -1)
347+
delegate?.tokenField?(self, didChangeInputText: text.substring(to: index))
343348
}
349+
344350
return true
345351
}
346352

0 commit comments

Comments
 (0)