Skip to content

Commit 081bfc9

Browse files
committed
Add tokenField(_:shouldCompleteText:)
1 parent 8fc5b2e commit 081bfc9

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

Example/CustomizedTokenViewController.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,18 @@ class CustomizedTokenViewController: UIViewController, ICTokenFieldDelegate {
8383
print(#function)
8484
}
8585

86+
func tokenField(_ tokenField: ICTokenField, shouldCompleteText text: String) -> Bool {
87+
print("Should add \"\(text)\"?")
88+
return text != "42"
89+
}
90+
8691
func tokenField(_ tokenField: ICTokenField, didCompleteText text: String) {
87-
print("Add: \"\(text)\"")
92+
print("Added \"\(text)\"")
8893
updateTexts()
8994
}
9095

9196
func tokenField(_ tokenField: ICTokenField, didDeleteText text: String, atIndex index: Int) {
92-
print("Delete: \"\(text)\"")
97+
print("Deleted \"\(text)\"")
9398
updateTexts()
9499
}
95100

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ See `Example/CustomizedTokenField.swift` for more details.
135135
* `tokenFieldDidBeginEditing(_:)`
136136
* `tokenFieldDidEndEditing(_:)`
137137
* `tokenFieldWillReturn(_:)`
138+
* `tokenField(_:shouldCompleteText:)`
138139
* `tokenField(_:didCompleteText:)`
139140
* `tokenField(_:didDeleteText:atIndex:)`
140141

Source/TokenField/ICTokenField.swift

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import UIKit
3434
@objc optional func tokenFieldDidEndEditing(_ tokenField: ICTokenField)
3535
/// Tells the delegate that the token field will process the pressing of the return button.
3636
@objc optional func tokenFieldWillReturn(_ tokenField: ICTokenField)
37+
/// Asks the delegate if the text should become a token in the token field.
38+
@objc optional func tokenField(_ tokenField: ICTokenField, shouldCompleteText text: String) -> Bool
3739
/// Tells the delegate that the text becomes a token in the token field.
3840
@objc optional func tokenField(_ tokenField: ICTokenField, didCompleteText text: String)
3941
/// Tells the delegate that the token at certain index is removed from the token field.
@@ -289,21 +291,25 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
289291
let text = (input as NSString).replacingCharacters(in: range, with: string)
290292

291293
for delimiter in delimiters {
292-
if text.hasSuffix(delimiter) {
293-
let index = text.index(text.endIndex, offsetBy: -delimiter.characters.count)
294-
let newToken = text.substring(to: index)
295-
textField.text = nil
296-
297-
if !newToken.isEmpty && newToken != delimiter {
298-
tokens.append(ICToken(text: newToken, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes))
299-
layoutTokenTextField()
300-
delegate?.tokenField?(self, didCompleteText: newToken)
301-
}
302-
togglePlaceholderIfNeeded()
294+
guard text.hasSuffix(delimiter) else {
295+
continue
296+
}
297+
298+
let index = text.index(text.endIndex, offsetBy: -delimiter.characters.count)
299+
let newToken = text.substring(to: index)
303300

304-
return false
301+
if !newToken.isEmpty && newToken != delimiter && (delegate?.tokenField?(self, shouldCompleteText: newToken) ?? true) {
302+
tokens.append(ICToken(text: newToken, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes))
303+
layoutTokenTextField()
304+
delegate?.tokenField?(self, didCompleteText: newToken)
305305
}
306+
307+
textField.text = nil
308+
togglePlaceholderIfNeeded()
309+
310+
return false
306311
}
312+
307313
return true
308314
}
309315

@@ -436,6 +442,12 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
436442
guard let text = inputTextField.text, !text.isEmpty else {
437443
return
438444
}
445+
446+
let shouldCompleteText = delegate?.tokenField?(self, shouldCompleteText: text) ?? true
447+
guard shouldCompleteText else {
448+
return
449+
}
450+
439451
inputTextField.text = nil
440452
tokens.append(ICToken(text: text, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes))
441453
layoutTokenTextField()

0 commit comments

Comments
 (0)