Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). Following the documentation I thought I can utilize the callbacks provided by the UITextInputDelegate protocol:

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

However, the selectionWill/DidChange callbacks are never called. If I change the selection in my text, the other callbacks are triggered (textWill/DidChange) instead. As this is already odd, the problem I have is that the textInput param is always nil. But I need it, as I would like to access selectedTextRange to solve my problem.

Any ideas how to retrieve the currently selected text and/or its range within a custom keyboard implementation?

like image 861
cweinberger Avatar asked Oct 04 '14 09:10

cweinberger


People also ask

Can you create a custom keyboard on iPhone?

The xKeyboard is a keyboard extension in which you can add special symbols and words as you needs. There are lots of unicode characters and special symbols you can use. You could use the custom keyboard you build in word, PPT, Excel, notes and other text apps on iPhone or iPad.

How do I add a language to my iPhone keyboard?

Add or remove a keyboard for another languageGo to Settings > General > Keyboard. Tap Keyboards, then do any of the following: Add a keyboard: Tap Add New Keyboard, then choose a keyboard from the list. Repeat to add more keyboards.


1 Answers

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?)

Your keyboard has no access to the text actually written by the user, because the UITextView is in an completely different app.

Source: https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html - "System Keyboard Features Unavailable to Custom Keyboards", paragraph 7

Because a custom keyboard can draw only within the primary view of its UIInputViewController object, it cannot select text. 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? Have you added UITextInputDelegate to the class's (do you write it like this in English?) header file? Then your delegate methods should be called... Also some example code could be useful ;)

like image 163
K. Biermann Avatar answered Sep 25 '22 14:09

K. Biermann