Skip to content

Commit 2ea8817

Browse files
committed
Manually notify the delegate of the changed input text when the delete key is pressed
1 parent 0dff815 commit 2ea8817

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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)