Current text selection in CustomKeyBoardExtension

落花浮王杯 提交于 2019-12-03 16:25:07

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 with textWillChange/textDidChange as per requirement.

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

Hope this will help you.

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