I want to have to occasionally insert text into the UITextView text object. For example, if the user presses the "New Paragraph" button I would like to insert a double newline instead of just the standard single newline.
How can I go about such? Do i have to read the string from UITextView, mutate it, and write it back? Then how would I know where the pointer was?
Thanks
This is a correct answer!
The cursor position is also right.
A scroll position is also right.
Since the
text
property ofUITextView
is immutable, you have to create a new string and set the text property to it. NSString has an instance method (-stringByAppendingString:
) for creating a new string by appending the argument to the receiver:For Swift 3.0
You can append text by calling method
insertText
ofUITextView
Instance.Example:
Here's how I implemented it and it seems to work nicely.
UITextview has an insertText method and it respects cursor position.
for example: