I have a UITextView and I want have two buttons. When the user taps the back button I want the cursor to move back 1 character. When they tap to forward button the cursor should move forward 1 character. How could I do this?
I have a UITextView and I want have two buttons. When the user taps
Share
I wrote it out for you, referencing the answer linked below. You can drop the code I wrote into a basic project and try it out.
The solution idea:
You can programmatically select a block of text by specifying the range of text to select (see reference). What’s a range, you ask? It’s basically a starting position and a length. Since you only care about the cursor position, you can ignore the length (i.e. keep it at zero) and adjust the starting position of the “selection” by +1 or -1.
Reference:
Can I select a specific block of text in a UITextField?
Code Explanation:
In the code below, there are two buttons (Forward and Back) and a target method for each of them that changes the cursor position in the UITextView. Note that
tvis the class variable/property for your UITextView (and needs to be declared as such).The Code: