1

I have a UITextView which allows some RichTextEditing. I want that whenever user taps on certain location of text, the cursor should move there and editing should be started from that location.

Issue: What happens is, when I drag the cursor to any location of my choice and write something cursor immediately moves to the end of text of UITextView and starts writing. I want edit to start from user tapped location or cursor location.

Following are the functions I am using.

-(void) textViewDidBeginEditing{
    //Can set the cursor or selected Range of textView here or tap gesture recognizer required?
    return true;
}
-(void) textViewDidEndEditing{
    return true;
}
-(void) textViewDidChangeTextInRange:(UITextView *) textView range:(NSMakeRange) range replacementText: (NSString *) text{
    //Sets attributed text of my TextView based on my input AccessoryView
    if(textView.text.lenght > range.location){
        //This is my point of concern where I want to start the editing
    }
    return false;
}

Excuse me for my code mistakes. I don't have access to my objectiveC code now. It was written to give you an idea what I am doing. I found this but could not get it to work. I am using UITextView it is using field. how to move cursor in UITextField after setting its value

Kindly guide me.

Community
  • 1
  • 1
Sahar
  • 53
  • 7

1 Answers1

0

There is some kind of Issue with Apple Functions. I added the code reference here how to move cursor in UITextField after setting its value in my function

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    //code from stackoverflow question
}

but it did not work. I then added the same code in another function and called it as selector from shouldChangeTextInRange and it worked !!!

Working Code:

- (void)setCursorToCorrectPosition
{
    //code from stackoverflow question
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    [self performSelector:@selector(setCursorToCorrectPosition) withObject:nil afterDelay:0.01];
}
Community
  • 1
  • 1
Sahar
  • 53
  • 7