UITextInputDelegate方法无法正常运行 [英] UITextInputDelegate methods not functioning correctly
问题描述
我现在正在开发iOS 8自定义键盘扩展程序,使用 UITextInputDelegate 方法存在一些问题。
I'm working on iOS 8 custom keyboard extension right now, and there are some issues in using UITextInputDelegate Methods.
这是否右:当用户长按打字区域时,应该调用 selectionWillChange:和 selectionDidChange:方法?只要文本字面改变,就应该调用 textWillChange:和 textDidChange:方法吗?
Does this right: selectionWillChange: and selectionDidChange: methods should be called when user long-presses typing area? And textWillChange: and textDidChange: methods should be called whenever the text is literally changing?
实际上,我是什么观察到的是,当我在文本输入区域中更改选择时,会调用textWillChange:和textDidChange:,并且我无法得到其他两个方法在什么条件下被调用的线索。如果有人知道这些委托方法的用法,请告诉我。
Actually, what I observed is that, when I changed selection in text input area, textWillChange: and textDidChange: are called, and I cannot get a clue that the other two methods are called in what condition. If anyone knows about the usage of these delegate methods, please let me know.
推荐答案
这对我不起作用......我目前正在做的只是使用 textWillChange
和 textDidChange
如你所说,当你改变选择...(他们被称为 BEFORE 和 AFTER )
然后比较:
self.textDocumentProxy。 documentContextBeforeInput
self.textDocumentProxy.documentContextAfterInput
从 BEFORE (textWillChange)到 AFTER (textDidChange)查看选择范围或长度是否完全改变。
It's not working for me either... what I am currently doing is just using textWillChange
and textDidChange
which does get called, as you mentioned, when you change your selection... (they get called BEFORE and AFTER)
And then comparing the: self.textDocumentProxy.documentContextBeforeInput
self.textDocumentProxy.documentContextAfterInput
From BEFORE (textWillChange) to the AFTER (textDidChange) to see if selection range or length changed at all.
这样的东西(在你的.h文件中设置下面的4个NSStrings ...当然没有测试过这个完整的片段,因为我刚才在SO.com上从头开始编写它,但我确信如果我犯了任何错误,原理是否有效) / p>
Something like this (set the 4 NSStrings below in your .h file of course... haven't tested this exact snippet because I wrote it from scratch just now on SO.com but I'm sure the principle works if I made any errors)
- (void)textWillChange:(id<UITextInput>)textInput {
beforeStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
beforeStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;
}
- (void)textDidChange:(id<UITextInput>)textInput {
afterStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
afterStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;
BOOL didSelectionChange = NO;
if (![beforeStringOfTextBehindCursor isEqualToString:afterStringOfTextBehindCursor]) {
didSelectionChange = YES;
}
if (![beforeStringOfTextAfterCursor isEqualToString:afterStringOfTextAfterCursor]) {
didSelectionChange = YES;
}
if (didSelectionChange) {
NSLog(@"Selection Changed!");
}
}
这篇关于UITextInputDelegate方法无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!