5

As we know, the original keyboard in iOS can delete whole words by holding down the delete button (⌫) for an extended period of time.
So how can we use the same functionality for custom keyboards in Swift, iOS 8?

Note:
I'm currently using proxy.deleteBackward() for deleting letters, and using:

var gesture = UILongPressGestureRecognizer(target: self, action: "longPressed:")
gesture.minimumPressDuration = 1.0
button.addGestureRecognizer(gesture)

when the button is pressed for a greater amount of time.

Thanks!

Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
He Yifei 何一非
  • 2,592
  • 4
  • 38
  • 69

1 Answers1

1

I'm not sure how you would be able to do it through gesture recognizer.

The original keyboard behaviour is,

  • When the button is pressed and kept pressed for initial X-time interval, it keeps deleting backward.
  • When the button is kept pressed after the initial X-time interval, it starts deleting words instead of just characters.

After the button is pressed the first time, you should probably keep calling your delete function and keep noticing if 'X-time-interval' has elapsed. Pseudocode would be

var startTime: NSDate = NSDate()
var timer: NSTimer?
func deleteButtonPressed(deleteButton: UIButton) {
    startTime = NSDate()
    timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("delete"), userInfo: nil, repeats: true)
}

func delete() {
    if !deleteButton.highlighted {
        timer.invalidate()
        timer = nil
        return
    }

    if ((currentNSDate - startTime ) < "X-time-Interval") {
        // delete backward
    } else {
        /* figure out last space character in text and create NSRange
        then
        mytextView.text deleteCharactersInRange:theRange
        set new text */
    }
}
He Yifei 何一非
  • 2,592
  • 4
  • 38
  • 69
Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • but I'm trying to build a custom keyboard Extension which can be use in other application (in whole system). How `mytextView.text deleteCharactersInRange:theRange` work then? – He Yifei 何一非 Jun 09 '15 at 02:40
  • DeleteCharactersInRange is a function for text. So it should work. If your keyboard works universally it should know which textField/textView it's editing. – Shamas S Jun 09 '15 at 07:06