i'm starting to develop a custom keyboard with iOS8 and objective-c. I'd like to create a delete button that deletes a single character, at insertion point, when single-pressed, and deletes entire words when it's hold pressed. the deleteBackward seems to delete a single character, but on Apple developers docs i've found:
To let you determine how much text is appropriate to delete when you call the deleteBackward method, obtain the textual context near the insertion point from the documentContextBeforeInput property of the textDocumentProxy property, as follows:
NSString *precedingContext = self.textDocumentProxy.documentContextBeforeInput;
You can then delete what you determine to be appropriate—for example, a single character, or everything back to a whitespace character.
But i don't understand how to achieve that... Is there a way to delete a complete word?
EDIT: I've found the solution... it's a simple tokenize on documentContextBeforeInput:
-(void)pressDeleteKey{
NSArray *tokens = [self.textDocumentProxy.documentContextBeforeInput componentsSeparatedByString:@" "];
for (int i = 0; i < [[tokens lastObject] length];i++) {
[self.textDocumentProxy deleteBackward];
}
}
Swift 1.2 Solution:
extension String {
var length : Int {
get{
return count(self)
}
}
}
func deleteLastWord() {
if let textArray:[String]? = proxy.documentContextBeforeInput?.componentsSeparatedByString(" "){
if let validArray = textArray{
for var i=0; i<validArray.last!.length; i++ {
proxy.deleteBackward()
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With