How to get a dummy cursor for iOS, using SwiftUI

Ben Sage
Oct 22, 2020

Context

This tutorial is for an iSwiftUI package. Learn more about iSwiftUI here.

A few months ago, I found myself trying to build one-time passcode input field for a login flow with SwiftUI. It wasn’t easy.

One time codes usually look like 6 or so different text fields that automatically switch to the next one when you type a number. The problem was that the SwiftUI TextField didn’t support programmatically switching text fields.

So my working solution was to make the text field transparent. And I made all the graphics just for show.

The obvious problem was the cursor. How do you get an iOS cursor just for show?

The misleadingly short answer is “timers.” Lots, and lots of timers.

So if you find yourself needing a dummy cursor for one-time code inputs or anything else, let me save you some agony.

Meet iDummyCursor

iDummyCursor does just about anything you’d need an on-screen cursor to do. It’s most easily installed via the swift package manager.

https://github.com/benjaminsage/iDummyCursor.git

If you haven’t done this kind of thing before, we’ve got detailed instructions on the process here.

Customizations

iDummyCursor supports the 2 things you need a cursor to do:

  1. Change color
  2. Change text size

(Yes, I mapped out the entire font size curve for the iOS cursor. That wasn’t nearly as difficult as getting the timing spot-on.)

For you, it should just work. Try an example like:

Conclusions

Please comment if you have any questions!

iDummyCursor documentation: https://iswiftui.com/iDummyCursor.html

iSwiftUI Home: iSwiftUI.com

--

--

Ben Sage

iOS programmer using SwiftUI, AKA IcyHovercraft