简体   繁体   English

自定义键盘:获取所选文本

[英]Custom Keyboard: get selected text

From my Custom Keyboard I would like to know if the user has selected something in their text (and the range of it). 通过我的Custom Keyboard我想知道用户是否在文本中选择了某项内容(及其范围)。 Following the documentation I thought I can utilize the callbacks provided by the UITextInputDelegate protocol: 根据文档,我认为我可以利用UITextInputDelegate协议提供的回调:

  • selectionWillChange(textInput: UITextInput) selectionWillChange(textInput:UITextInput)
  • selectionDidChange(textInput: UITextInput) selectionDidChange(textInput:UITextInput)
  • textWillChange(textInput: UITextInput) textWillChange(textInput:UITextInput)
  • textDidChange(textInput: UITextInput) textDidChange(textInput:UITextInput)

However, the selectionWill/DidChange callbacks are never called. 但是,从不调用selectionWill/DidChange回调。 If I change the selection in my text, the other callbacks are triggered ( textWill/DidChange ) instead. 如果更改文本中的选择,则会触发其他回调( textWill/DidChange )。 As this is already odd, the problem I have is that the textInput param is always nil. 由于这已经很奇怪了,所以我textInput的问题是textInput参数始终为nil。 But I need it, as I would like to access selectedTextRange to solve my problem. 但是我需要它,因为我想访问selectedTextRange来解决我的问题。

Any ideas how to retrieve the currently selected text and/or its range within a custom keyboard implementation? 有什么想法如何在自定义键盘实现中检索当前选择的文本和/或其范围?

If I'm not completely wrong, this is impossible. 如果我不完全错,那是不可能的。 (You're referring to an own keyboard app on iOS 8; not an implementation of your own keyboard in your own app, right?) (您指的是iOS 8上自己的键盘应用程序;不是您自己的应用程序中自己键盘的实现,对吗?)

Your keyboard has no access to the text actually written by the user, because the UITextView is in an completely different app. 您的键盘无权访问用户实际编写的文本,因为UITextView在完全不同的应用程序中。

Source: https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html - "System Keyboard Features Unavailable to Custom Keyboards", paragraph 7 来源: https : //developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html- “自定义键盘无法使用的系统键盘功能”,第7段

Because a custom keyboard can draw only within the primary view of its UIInputViewController object, it cannot select text. 因为自定义键盘只能在其UIInputViewController对象的主视图内绘制,所以它不能选择文本。 Text selection is under the control of the app that is using the keyboard. 文本选择在使用键盘的应用程序的控制下。 If that app provides an editing menu interface (such as for Cut, Copy, and Paste), the keyboard has no access to it. 如果该应用程序提供了编辑菜单界面(例如剪切,复制和粘贴),则键盘无法访问它。 A custom keyboard cannot offer inline autocorrection controls near the insertion point. 自定义键盘不能在插入点附近提供嵌入式自动更正控件。


If you are referring to your own implementation in your own app: have you selected the correct class as textfield delegate in your IB? 如果您在自己的应用程序中引用自己的实现:您是否在IB中选择了正确的类作为文本字段委托? Have you added UITextInputDelegate to the class's (do you write it like this in English?) header file? 您是否已将UITextInputDelegate添加到类的(您是否用英语这样写?)头文件中? Then your delegate methods should be called... Also some example code could be useful ;) 然后应该调用您的委托方法...还有一些示例代码可能很有用;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM