Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete entire word with deleteBackward in custom keyboard on iOS 8

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];
}

}

like image 319
xabaras78 Avatar asked Oct 14 '14 14:10

xabaras78


1 Answers

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()
                        }
                }
        }
}
like image 120
Statik Avatar answered Sep 21 '22 21:09

Statik