加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – CustomKeyBoardExtension中的当前文本选择

发布时间:2020-12-16 07:09:51 所属栏目:百科 来源:网络整理
导读:我正在尝试编写自定义键盘扩展. 我正在寻找方法来了解光标在CustomKeyboardExtension中的UITextField,UITextView等等的位置…但我没有看到类似的东西. 我看到SwiftKey应用程序(http://swiftkey.com)可以做到(或做类似的事情).当我更改光标时,建议文本将会改
我正在尝试编写自定义键盘扩展.

我正在寻找方法来了解光标在CustomKeyboardExtension中的UITextField,UITextView等等的位置…但我没有看到类似的东西.

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

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

更新:29/09/2014

好吧,我太傻了.我们可以使用textContextBeforeInput,textDocumentAxyInput方法的textDocumentProxy属性.我以为“之前”,“之后”是关于时间的.实际上这是关于这个位置的.

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

解决方法

创建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]);
}

希望这会帮助你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读