When making a custom iOS keyboard extension, how can I create a button that has the same functionality as Apple's original backspace key?

两盒软妹~` 提交于 2019-12-03 20:11:25

I Found the answer from HERE.

In this keyBoard the deleteButton have same functionality as I want.

Code for that button is:

private func addDeleteButton() {
    deleteButton = KeyButton(frame: CGRectMake(keyWidth * 8.5 + spacing * 9.5, keyHeight * 2.0 + spacing * 3.0 + predictiveTextBoxHeight, keyWidth * 1.5, keyHeight))
    deleteButton.setTitle("\u{0000232B}", forState: .Normal)
    deleteButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: .TouchUpInside)
    self.view.addSubview(deleteButton)

    let deleteButtonLongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPressForDeleteButtonWithGestureRecognizer:")
    deleteButton.addGestureRecognizer(deleteButtonLongPressGestureRecognizer)

    let deleteButtonSwipeLeftGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipeLeftForDeleteButtonWithGestureRecognizer:")
    deleteButtonSwipeLeftGestureRecognizer.direction = .Left
    deleteButton.addGestureRecognizer(deleteButtonSwipeLeftGestureRecognizer)
}

func deleteButtonPressed(sender: KeyButton) {
    switch proxy.documentContextBeforeInput {
    case let s where s?.hasSuffix("    ") == true: // Cursor in front of tab, so delete tab.
        for i in 0..<4 { // TODO: Update to use tab setting.
            proxy.deleteBackward()
        }
    default:
        proxy.deleteBackward()
    }
    updateSuggestions()
}

func handleLongPressForDeleteButtonWithGestureRecognizer(gestureRecognizer: UILongPressGestureRecognizer) {
    switch gestureRecognizer.state {
    case .Began:
        if deleteButtonTimer == nil {
            deleteButtonTimer = NSTimer(timeInterval: 0.1, target: self, selector: "handleDeleteButtonTimerTick:", userInfo: nil, repeats: true)
            deleteButtonTimer!.tolerance = 0.01
            NSRunLoop.mainRunLoop().addTimer(deleteButtonTimer!, forMode: NSDefaultRunLoopMode)
        }
    default:
        deleteButtonTimer?.invalidate()
        deleteButtonTimer = nil
        updateSuggestions()
    }
}

func handleSwipeLeftForDeleteButtonWithGestureRecognizer(gestureRecognizer: UISwipeGestureRecognizer) {
    // TODO: Figure out an implementation that doesn't use bridgeToObjectiveC, in case of funny unicode characters.
    if let documentContextBeforeInput = proxy.documentContextBeforeInput as NSString? {
        if documentContextBeforeInput.length > 0 {
            var charactersToDelete = 0
            switch documentContextBeforeInput {
            case let s where NSCharacterSet.letterCharacterSet().characterIsMember(s.characterAtIndex(s.length - 1)): // Cursor in front of letter, so delete up to first non-letter character.
                let range = documentContextBeforeInput.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet().invertedSet, options: .BackwardsSearch)
                if range.location != NSNotFound {
                    charactersToDelete = documentContextBeforeInput.length - range.location - 1
                } else {
                    charactersToDelete = documentContextBeforeInput.length
                }
            case let s where s.hasSuffix(" "): // Cursor in front of whitespace, so delete up to first non-whitespace character.
                let range = documentContextBeforeInput.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet().invertedSet, options: .BackwardsSearch)
                if range.location != NSNotFound {
                    charactersToDelete = documentContextBeforeInput.length - range.location - 1
                } else {
                    charactersToDelete = documentContextBeforeInput.length
                }
            default: // Just delete last character.
                charactersToDelete = 1
            }

            for i in 0..<charactersToDelete {
                proxy.deleteBackward()
            }
        }
    }
    updateSuggestions()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!