我正在制作一个customKeyboard,它有删除按钮,用于删除文本和代码:
func addDelete() {
deleteButton = UIButton.buttonWithType(.System) as UIButton
deleteButton.setTitle(" Delete ", forState: .Normal)
deleteButton.sizeToFit()
deleteButton.setTranslatesAutoresizingMaskIntoConstraints(false)
deleteButton.addTarget(self, action: "didTapDelete", forControlEvents: .TouchUpInside)
deleteButton.layer.cornerRadius = 5
view.addSubview(deleteButton)
var rightSideConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0)
var topConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: +10.0)
view.addConstraints([rightSideConstraint, topConstraint])
}
func didTapDelete() {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.deleteBackward()
}
但这很慢,因为我必须点击这么多时间才能删除textField
中的大量字符。
有没有办法创建一个我可以连续按下的按钮,它可以像默认的iPhone keyBoard backSpace按钮一样快速删除一个角色吗?
答案 0 :(得分:5)
我从HERE找到答案。
在这个keyBoard中,deleteButton具有我想要的相同功能。
该按钮的代码是:
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()
}