3

Is there any simple way to insert text at the caret position in a UITextField? I have a custom keypad with some special characters and I'm having difficulty making this work. I do not allow arbitrary input, the keypad is limited to about 20 special characters, and all others are rejected.

The method of using the global clipboard to insert is a bad one since with VoiceOver turned on it says "Paste" instead of the character inserted every time you use it. Which is not good enough.

Finding any alternate solutions seems quite difficult since all just reference the paste solution.

Community
  • 1
  • 1
Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29

2 Answers2

6

The solution was simpler than I thought:

[self insertText:string];
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, string);

This will work properly with selection, caret position et. al.

It will however not use the proper tone when announcing the entered string. VoiceOver uses a deeper voice when announcing inserted text, I put up a separate question on how to control the tone of VoiceOver.

Community
  • 1
  • 1
Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29
0

A UITextField implements the UITextInput protocol, and thus has a 'selectedTextRange' property, which returns either the selection or the cursor location. Read up on it as you can get lots of interesting information from the methods required in that protocol.

David H
  • 40,852
  • 12
  • 92
  • 138