To create a custom keyboard, start by subclassing the
UIInputViewController class. Add your keyboard’s user interface to the
inputView property of your subclass.
- iOS 8.0+
- tvOS 8.0+
Xcode 6 and later provides a starting point for a custom keyboard: the Custom Keyboard target template.
A custom keyboard can respond to user input events in the following ways:
Add text in the form of an unattributed
NSStringobject at the insertion point in the current text input object, by calling the
insertText(_:)method on the
textDocumentProxyproperty. This property provides that method through its conformance to the
Switch to another keyboard in the set of user-enabled keyboards, by calling the
Dismiss the keyboard, by calling the
Obtain textual context around the insertion point by reading the
documentContextAfterInput. To find out if the current text input object is empty, call the
hasText method on the
textDocumentProxy property. You can employ this textual context by considering it along with user input, to offer context-sensitive output to a document from your keyboard.
An input view controller conforms to the
UITextInputDelegate protocol, allowing you to respond to changes in document content and position of the insertion point.
To present an appropriate keyboard layout, respond to the current text input object’s
UIKeyboardType property. For each keyboard type trait you support, change the contents of your primary view accordingly.