0
votes

I have a string in a UITextView.

NSString *str = @"Hello world. What @are you @doing ?" 

When I tap on the UITextView the cursor goes where I tap on the string. But I need that, suppose I tap on any character of that textview, the cursor will automatically goes to the end of that word. For e.g, I tap on the character "e" in "Hello", then the cursor will place after "Hello".

How can I do this?

2
Try modify textView.selectedRange? Get the range (i.e. index) and compute next none white space character?zc246
Hi, Thank you for your answer. Cursor behave properly. But is any word contains any special characters (like : hello_01) then the cursor goes on @"_". How can I prevent this? How can I treat this as normal string, so that the cursor goes at the end?Samuel Patterson
Exactly, you need to find the next white space yourself, and set the selectedRangezc246

2 Answers

1
votes

You have to understand the concept and underlying structure of a touch device. Let me explain. When you have a textfield/ textview, inside a view and your keyboard is not showing ie. textfield is inactive. The superview might have any number of gesture recognisers other than the textfield. Hence the OS just detects the touch which will make the UI element become firstResponder, or rather Active. After the textfield is active, OS recognises that the other gesture pattern will come into play in this moment. And in this moment you can tap in any position (or Long tap) and then place ur cursor at any character you want.

Hope you could understand.

0
votes

this should be the default behavior of textview. You not need to do any additional setup for that! If you will long press then only your cursor go at particular character otherwise it will go to end of the word. this is the default behavior.