@@ -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