TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to create custom text input using UIKeyInput

Swift version: 5.10

Paul Hudson    @twostraws   

Although we can use pressesBegan() and pressesEnded() to read keypresses, they won’t show the on-screen keyboard and so won’t let you provide custom text input for users without a hardware keyboard. If you need that keyboard to be shown, you should create a class that adopts the UIKeyInput protocol instead, which has just three requirements:

  • What to do when text is inserted.
  • What to do when text is deleted.
  • Whether your custom text input currently has text or not.

The only other thing you need to know is that your custom input control will show the keyboard when it becomes first responder. So, you should override the canBecomeFirstResponder property of your subclass, setting it to true rather than the default of false.

To demonstrate this, we could create a simple UIView subclass that draws text to the screen as it’s typed, like this:

class TextRenderingView: UIView, UIKeyInput {
    // the string we'll be drawing
    var input = ""

    override var canBecomeFirstResponder: Bool {

    var hasText: Bool {
        input.isEmpty == false

    func insertText(_ text: String) {
        input += text

    func deleteBackward() {
        _ = input.popLast()

    override func draw(_ rect: CGRect) {
        let attrs: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 32)]
        let attributedString = NSAttributedString(string: input, attributes: attrs)
        attributedString.draw(in: rect)

If you want to handle more complex user input, such as selecting ranges of text or drawing the caret, you should use the more advanced UITextInput protocol instead.

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 3.2

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.9/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.