我正在尝试写Custom Keyboard Extension
.
我正在寻找知道光标在哪里的方法UITextField
,UITextView
...等等自定义键盘扩展...但我没有看到类似的东西。
我看到了 SwiftKey 应用程序(http://swiftkey.com http://swiftkey.com)可以做到这一点(或做类似的事情)。当我改变光标时,建议文本将会改变(见下图)。
Q:我们如何获得当前的文本选择?
...
更新:2014 年 9 月 29 日
好吧,我就是这么傻。我们可以用documentContextBeforeInput
, documentContextAfterInput
的方法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]);
}
希望对你有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)