CustomKeyBoardExtension中的当前文本选择 [英] Current text selection in CustomKeyBoardExtension
问题描述
我正在尝试编写 自定义键盘扩展程序
。
I'm trying to write Custom Keyboard Extension
.
我'我在 CustomKeyboardExtension中寻找知道光标位于 UITextField
, UITextView
...等的方法 ...但我没有看到类似的东西。
I'm looking for the way to know where the cursor is on UITextField
,UITextView
...etc in CustomKeyboardExtension ... but I don't see anything like that.
我看到了SwiftKey app( http://swiftkey.com )可以做到(或做类似的事情)。当我更改光标时,建议文本将会改变(见下图)。
I saw SwiftKey app (http://swiftkey.com) can do that (or do something like that). When I change the cursor, suggestion-text will change (see below pictures).
问:我们如何才能获得当前文本选择?
Q: How can we get current text selection?
...
更新日期:29/09/2014
好的,我太傻了。我们可以使用 documentContextBeforeInput
, documentContextAfterInput
textDocumentProxy
属性的方法。我以为之前,之后是关于时间的。实际上这是关于这个位置。
Ok, I'm so foolish. We can use documentContextBeforeInput
, documentContextAfterInput
methods of textDocumentProxy
property. I thought that "Before","After" are about the time. Actually it's about the position.
对不起所有!我浪费你的时间:(
Sorry all! I wasted your time :(
推荐答案
创建lastWordBeforeInput方法......
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 @"";
}
然后打电话给我t根据要求使用textWillChange / textDidChange。
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]);
}
希望这会对你有帮助。
这篇关于CustomKeyBoardExtension中的当前文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!