UITextView: insertText() method
The insertText() method inserts text as if the user typed it: at the cursor position, or, if text is selected, replacing the selection. The cursor moves to after the inserted content.
Syntax
insertText(text)Parameters
text
The text to insert, a string. Other types are converted with String(); null and undefined are treated as the empty string.
Return value
None (undefined).
Exceptions
None.
Description
- Fires
inputonce, and also firesselectionchange. Inserting an empty string changes nothing but still firesinput(measured). - It still inserts when
isEditableisfalse(measured). - Callbacks run a little later: on the line after calling
insertText(), theinputcallback hasn't run yet.
Examples
js
const input = new UITextView();
input.text = 'Hello';
input.insertText('😀');
console.log(input.text);Result: logs Hello😀. After text is written the cursor is at the end, so the emoji is inserted at the end.
Corresponding iOS API
-[UITextView insertText:] (the UIKeyInput protocol).
See also
deleteBackward()inputevent