从 UITextfield 的开头删除将清除整个文本字段

Deleting from the start of the UITextfield clears the whole textfield

提问人:Dawood Aamir 提问时间:3/20/2023 最后编辑:HangarRashDawood Aamir 更新时间:3/21/2023 访问量:151

问:

当光标位于 UITextfield 的开头时,按一次 delete 键将清除整个文本字段。为什么会出现此问题,我该如何解决?

下面是我用于 UITextfield 的代码,用于根据需要修改其属性。我尝试了几种解决方案,但似乎没有任何效果。此行为在 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
    }
}
iOS Swift Cocoa-Touch UIKit的

评论

0赞 Maziar Saadatfar 3/20/2023
你能检查一下吗,“yourTextfield.clearsOnBeginEditing = false”
0赞 DonMag 3/20/2023
“按一次删除是什么意思?你是说退格吗?快速测试,使用您的代码而不进行任何更改,并且您描述的内容不会发生在我身上......
0赞 DonMag 3/20/2023
如果已设置 ,则这是设计的行为。.isSecureTextEntry = true
0赞 Dawood Aamir 3/22/2023
Textfield.clearsOnBeginEditing 为 false
0赞 Dawood Aamir 3/22/2023
@DonMag 是的,退格键。

答: 暂无答案