Add a Clear button functionality in Custom Keyboard using Swift

923 Views Asked by At

I use a custom keyboard which contains a delete and a clear button.I can able to delete the letters using this code

let obj =  UIInputViewController()
(obj.textDocumentProxy as UIKeyInput).deleteBackward()

Is it any keyword available to clear whole text in a textfield.

NB: I found this link but it is not apt for my requirement

1

There are 1 best solutions below

1
KrishnaCA On BEST ANSWER

There is no keyword available for deleting the whole text in the textfield through custom keyboard.

To clear all the text that the UITextDocumentProxy object can access in the textfield before the cursor position

if let word:String = self.textDocumentProxy.documentContextBeforeInput     
{
    for _: Int in 0 ..< word.characters.count {
        self.textDocumentProxy.deleteBackward()
    }
}

To delete the text after cursor position, you have to move the cursor forward to delete it.