How do you get the cursor to appear programmatically in a custom UITextInput with UITextInteraction?

I have created a custom input field by conforming to UITextInput. It is setup to use UITextInteraction. Everything works very well. If the user taps on the custom field, the cursor (provided by UITextInteraction) appears. The user can type, select, move the cursor, etc.

But I'm stumped trying to get the cursor to appear automatically. With a normal UITextField or UITextView you simply call becomeFirstResponder(). But doing that with my custom UITextInput does not result in the cursor appearing. It only appears if the user taps on the custom field.

I don't know what I'm missing. I don't see any API in UITextInteraction that can be called to say "activate the cursor layer".

Does anyone know what steps are required with a custom UITextInput using UITextInteraction to activate the cursor programmatically without the user needing to tap on the custom field?

Replies

Interesting! I also have some code that has for years used UITextInput on a custom view; I was drawing a cursor myself.

I've just tried - with a very quick hack - to add UITextInteraction. It seems promising, not least because it provides the loupe (magnifier) functionality. But as you say, the cursor ("beam") is not initially shown. I can enter characters and it does call my caretRectForPosition method for the position where the cursor should be, but it doesn't draw it until I tap somewhere.

I'll let you know if I discover anything useful.

  • I’ve made attempts to generate touch events or directly call target/action pairs of the gestures provided by UITextInteraction but so far no luck. Too many private APIs involved for me to find a working hack along those lines.

Add a Comment