【问题标题】:Current text selection in CustomKeyBoardExtension自定义键盘扩展中的当前文本选择
【发布时间】:2014-09-25 04:23:28
【问题描述】:

我正在尝试写 Custom Keyboard Extension

我正在寻找知道光标在 CustomKeyboardExtensionUITextFieldUITextView...等上的位置的方法...但我没有看到类似的东西。

我看到 SwiftKey 应用程序 (http://swiftkey.com) 可以做到这一点(或做类似的事情)。当我改变光标时,建议文本会改变(见下图)。

问:我们如何获得当前的文本选择?

...

更新:2014 年 9 月 29 日

好吧,我太傻了。我们可以使用textDocumentProxy 属性的documentContextBeforeInputdocumentContextAfterInput 方法。我认为“之前”,“之后”是时候了。其实是职位的问题。

对不起!我浪费了你的时间:(

【问题讨论】:

  • textDocumentProxy.documentContextBeforeInput
  • 只是 'documentContextBeforeInput' 和 'documentContextAfterInput' 也不会删减它。如果用户打开一个自动选择的文本,两者都将为空,但该字段中肯定有文本。

标签: objective-c ios8 uiinputviewcontroller ios8-extension


【解决方案1】:

创建 lastWordBeforeInput 方法...

-(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 @"";
}

然后根据需要使用 textWillChange/textDidChange 调用它。

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

希望这会对你有所帮助。

【讨论】:

  • 不回答问题:“问:我们如何获得当前的文本选择?”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多