@@ -34,8 +34,12 @@ 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+ /// Tells the delegate the input text is changed.
38+ @objc optional func tokenField( _ tokenField: ICTokenField , didChangeInputText text: String )
39+ /// Asks the delegate if the text should become a token in the token field.
40+ @objc optional func tokenField( _ tokenField: ICTokenField , shouldCompleteText text: String ) -> Bool
3741 /// Tells the delegate that the text becomes a token in the token field.
38- @objc optional func tokenField( _ tokenField: ICTokenField , didEnterText text: String )
42+ @objc optional func tokenField( _ tokenField: ICTokenField , didCompleteText text: String )
3943 /// Tells the delegate that the token at certain index is removed from the token field.
4044 @objc optional func tokenField( _ tokenField: ICTokenField , didDeleteText text: String , atIndex index: Int )
4145}
@@ -287,23 +291,28 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
287291 }
288292
289293 let text = ( input as NSString ) . replacingCharacters ( in: range, with: string)
294+ delegate? . tokenField ? ( self , didChangeInputText: text)
290295
291296 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 , didEnterText: newToken)
301- }
302- togglePlaceholderIfNeeded ( )
297+ guard text. hasSuffix ( delimiter) else {
298+ continue
299+ }
300+
301+ let index = text. index ( text. endIndex, offsetBy: - delimiter. characters. count)
302+ let newToken = text. substring ( to: index)
303303
304- return false
304+ if !newToken. isEmpty && newToken != delimiter && ( delegate? . tokenField ? ( self , shouldCompleteText: newToken) ?? true ) {
305+ tokens. append ( ICToken ( text: newToken, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes) )
306+ layoutTokenTextField ( )
307+ delegate? . tokenField ? ( self , didCompleteText: newToken)
305308 }
309+
310+ textField. text = nil
311+ togglePlaceholderIfNeeded ( )
312+
313+ return false
306314 }
315+
307316 return true
308317 }
309318
@@ -436,10 +445,16 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
436445 guard let text = inputTextField. text, !text. isEmpty else {
437446 return
438447 }
448+
449+ let shouldCompleteText = delegate? . tokenField ? ( self , shouldCompleteText: text) ?? true
450+ guard shouldCompleteText else {
451+ return
452+ }
453+
439454 inputTextField. text = nil
440455 tokens. append ( ICToken ( text: text, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes) )
441456 layoutTokenTextField ( )
442- delegate? . tokenField ? ( self , didEnterText : text)
457+ delegate? . tokenField ? ( self , didCompleteText : text)
443458 }
444459
445460 /// Removes the input text and all displayed tokens.
0 commit comments