Current text selection in CustomKeyBoardExtension

I'm trying to write . Custom Keyboard Extension

I am looking for a way to find out where the cursor is on UITextField, UITextView... etc. in CustomKeyboardExtension ... but I don't see anything like it.

I saw a SwiftKey application ( http://swiftkey.com ) can do this (or do something similar). When I change the cursor, the text of the sentence will change (see the figures below).

Q: How can we get the current selection of text?

...

UPDATE: 09/29/2014

OK, I'm so stupid. We can use methods documentContextBeforeInput, documentContextAfterInputproperties textDocumentProxy. I thought Before, After, is the time. In fact, this is about the position.

Excuse me! I wasted your time :(

+4
source share
1 answer

Create lastWordBeforeInput method ...

-(NSString *) lastWordBeforeInput{
    NSArray *arrayOfSplitsString = [self.textDocumentProxy.documentContextBeforeInput componentsSeparatedByString:@" "];
    int countIndex = arrayOfSplitsString.count - 1;
    NSCharacterSet *ChSet = [NSCharacterSet alphanumericCharacterSet];
    NSCharacterSet *invertedChSet = [ChSet invertedSet];
    while (countIndex > 0) {
        NSString *lastWordOfSentance = [arrayOfSplitsString objectAtIndex:countIndex--];
        if ([[lastWordOfSentance stringByTrimmingCharactersInSet:invertedChSet] rangeOfCharacterFromSet:ChSet].location != NSNotFound) {
            return [lastWordOfSentance stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        }
    }

    return @"";
}

Then call it using textWillChange / textDidChange as required.

- (void)textWillChange:(id<UITextInput>)textInput {
    // The app is about to change the document contents. Perform any preparation here.
    NSLog(@"%@",[self lastWordBeforeInput]);
}

Hope this helps you.

+1
source

All Articles