@@ -34,8 +34,12 @@ import UIKit
34
34
@objc optional func tokenFieldDidEndEditing( _ tokenField: ICTokenField )
35
35
/// Tells the delegate that the token field will process the pressing of the return button.
36
36
@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
37
41
/// 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 )
39
43
/// Tells the delegate that the token at certain index is removed from the token field.
40
44
@objc optional func tokenField( _ tokenField: ICTokenField , didDeleteText text: String , atIndex index: Int )
41
45
}
@@ -287,23 +291,28 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
287
291
}
288
292
289
293
let text = ( input as NSString ) . replacingCharacters ( in: range, with: string)
294
+ delegate? . tokenField ? ( self , didChangeInputText: text)
290
295
291
296
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)
303
303
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)
305
308
}
309
+
310
+ textField. text = nil
311
+ togglePlaceholderIfNeeded ( )
312
+
313
+ return false
306
314
}
315
+
307
316
return true
308
317
}
309
318
@@ -436,10 +445,16 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
436
445
guard let text = inputTextField. text, !text. isEmpty else {
437
446
return
438
447
}
448
+
449
+ let shouldCompleteText = delegate? . tokenField ? ( self , shouldCompleteText: text) ?? true
450
+ guard shouldCompleteText else {
451
+ return
452
+ }
453
+
439
454
inputTextField. text = nil
440
455
tokens. append ( ICToken ( text: text, normalAttributes: normalTokenAttributes, highlightedAttributes: highlightedTokenAttributes) )
441
456
layoutTokenTextField ( )
442
- delegate? . tokenField ? ( self , didEnterText : text)
457
+ delegate? . tokenField ? ( self , didCompleteText : text)
443
458
}
444
459
445
460
/// Removes the input text and all displayed tokens.
0 commit comments