0

When user click the passage in textView, I want to change the cursor to the linebreak, but when I change selectionRange is always failed. I know the reason, but I must change the selectedRange in func: textViewDidChangeSelection:(UITextView*)textView

How can I change the selectedRange?

here is the code

-(void)textViewDidChangeSelection:(UITextView *)textView

//    paragLocations:   it contain all "\n" locations
NSArray* paragLocations = [self ParagraphLocationsWithText:textView Pattern:translatePragraphLinebreak];

//     location :According to user selection,The nearest "\n" location
NSUInteger nearestLocation = [self ClickParagraphEndBreakLoctionWithSelectLocation:textView.selectedRange.location withParagLocations:paragLocations];
//Then
//1. here I change the textView.selectedRange
textView.selectedRange = NSMakeRange(nearestLocation, 0);
//2. here I change the cursorPosition
CGFloat cursorPosition = [self caretRectForPosition:textView.selectedTextRange.start].origin.y;
[textView setContentOffset:CGPointMake(0, cursorPosition) animated:YES];
// but In step 1  ,It changed textView.selectedRange,So this func will do it again,and then again,until the nearestLocation became the paragLocations.lastObject.
/*
        so the question is how to break this Infinite loop ?  
        should I change selectedRange In this func?
        I want to changed the selectedRange base on User select In textview
 */

sorry about my poor english.

姜思宇
  • 1
  • 1

1 Answers1

0

You can try the following code, it works for me fine.

- (void)textViewDidChangeSelection:(UITextView *)textView {

       UITextRange *selectRange = [textView selectedTextRange];
       NSString *selectedText = [textView textInRange:selectRange];
    }
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40