When the cursor is placed at the start of a UITextfield, pressing delete once, clears the whole textfield. Why does this issue occur and how can I fix this?
Below is the code I'm using for the UITextfield to modify its properties according to my needs. I've tried several solutions but nothing seems to work. Is this behaviour default in iOS?
extension LockVC : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Reset the flag variable when editing begins
hasBeenEdited = false
}
func textFieldDidEndEditing(_ textField: UITextField) {
// Reset the flag variable when editing ends
hasBeenEdited = false
}
// When the user changes the text in the text field, limit the length of the text and handle deletion and insertion of characters
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let MAX_LENGTH = 25
// Ensure that there is a selected text range in the text field
guard let selectedRange = textField.selectedTextRange else {
return false
}
// Check if there is any selected text to delete
if selectedRange.start != selectedRange.end {
textField.deleteBackward()
return false
}
// If the delete key is pressed, handle deletion of text
if string.isEmpty && range.length == 1 {
// Get cursor position
guard let cursorPosition = textField.selectedTextRange?.start else {
return false
}
// If cursor is at start and text field has been previously edited, do not allow deletion
if cursorPosition == textField.beginningOfDocument && hasBeenEdited {
// If the cursor is at the beginning of the textfield, do not allow deletion
if range.location == 0 {
return false
}
}
// Delete only one character at cursor position
if let cursorPosition = textField.selectedTextRange?.start,
cursorPosition != textField.beginningOfDocument {
if let deletedRange = textField.textRange(from: cursorPosition, to: textField.position(from: cursorPosition, offset: -1)!) {
textField.replace(deletedRange, withText: "")
}
return false
}
return false
}
// Insertion
if let text = textField.text, !string.isEmpty || !text.isEmpty {
let currentText = text as NSString
let newText = currentText.replacingCharacters(in: range, with: string)
// If new text exceeds max length, show alert and do not allow insertion
if newText.count > MAX_LENGTH {
//self.showToast(message: "Password must be \(MAX_LENGTH) characters or less.", font: .systemFont(ofSize: 12.0))
let alert = UIAlertController(title: "Password must be \(MAX_LENGTH) characters or less.", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
return false
}
// Insert only one character at cursor position
if let cursorPosition = textField.selectedTextRange?.start {
if let insertionRange = textField.textRange(from: cursorPosition, to: cursorPosition) {
textField.replace(insertionRange, withText: string)
}
}
}
// Set the flag variable to true after first edit
if !hasBeenEdited {
hasBeenEdited = true
}
return false
}
}