HomeiOS Developmentios - Deleting from the beginning of the UITextfield clears the entire...

ios – Deleting from the beginning of the UITextfield clears the entire textfield


When the cursor is positioned firstly of UITextfield, urgent delete as soon as, clears the entire textfield. Why does this difficulty happen and the way can I repair this?

Beneath is the code I am utilizing for the UITextfield to change it is properties in response to my wants. I’ve tried a number of options however nothing appears to work. Is that this behaviour default in iOS?

<pre><code>
extension LockVC : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        // Reset the flag variable when modifying begins
        hasBeenEdited = false
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        // Reset the flag variable when modifying ends
        hasBeenEdited = false
    }
    
// When the person modifications the textual content within the textual content discipline, restrict the size of the textual content and deal with deletion and insertion of characters
    func textField(_ textField: UITextField, shouldChangeCharactersIn vary: NSRange,     replacementString string: String) -> Bool {
        let MAX_LENGTH = 25
        
        // Guarantee that there's a chosen textual content vary within the textual content discipline
        guard let selectedRange = textField.selectedTextRange else {
            return false
        }
        
        // Examine if there's any chosen textual content to delete
        if selectedRange.begin != selectedRange.finish {
            textField.deleteBackward()
            return false
        }
        
        // If the delete secret's pressed, deal with deletion of textual content
        if string.isEmpty && vary.size == 1 {
            // Get cursor place
            guard let cursorPosition = textField.selectedTextRange?.begin else {
                return false
            }
            
            // If cursor is at begin and textual content discipline has been beforehand edited, don't enable deletion
            if cursorPosition == textField.beginningOfDocument && hasBeenEdited {
                // If the cursor is at the start of the textfield, don't enable deletion
                if vary.location == 0 {
                    return false
                }
            }
            
            // Delete just one character at cursor place
            if let cursorPosition = textField.selectedTextRange?.begin,
               cursorPosition != textField.beginningOfDocument {
                if let deletedRange = textField.textRange(from: cursorPosition, to: textField.place(from: cursorPosition, offset: -1)!) {
                    textField.exchange(deletedRange, withText: "")
                }
                return false
            }
            return false
        }
        
        // Insertion
        if let textual content = textField.textual content, !string.isEmpty || !textual content.isEmpty {
            let currentText = textual content as NSString
            let newText = currentText.replacingCharacters(in: vary, with: string)
            
            // If new textual content exceeds max size, present alert and don't enable insertion
            if newText.rely > MAX_LENGTH {
                //self.showToast(message: "Password should be (MAX_LENGTH) characters or much less.", font: .systemFont(ofSize: 12.0))
                let alert = UIAlertController(title: "Password should be (MAX_LENGTH) characters or much less.", message: "", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", type: .default, handler: nil))
                current(alert, animated: true, completion: nil)
                return false
            }
            
            // Insert just one character at cursor place
            if let cursorPosition = textField.selectedTextRange?.begin {
                if let insertionRange = textField.textRange(from: cursorPosition, to: cursorPosition) {
                    textField.exchange(insertionRange, withText: string)
                }
            }
        }
        
        // Set the flag variable to true after first edit
        if !hasBeenEdited {
            hasBeenEdited = true
        }
        
        return false
    }
}
</code></pre>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments