2016-09-22 3 views
0

ユーザーがtextViewで項目をクリックすると、カーソルをlinebreakに変更しますが、変更すると常にselectionRangeは失敗します。 私はその理由を知っていますが、selectedRangefunc: textViewDidChangeSelection:(UITextView*)textViewに変更する必要がありますUITextView change selectRange alwaysクラッシュ

selectedRangeを変更するにはどうすればよいですか?ここ

はコードの私の下手な英語について申し訳ありません

-(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 
*/ 

です。

+0

"change ** selectionRange **は常に失敗"またはselectedRange ..ですか? – vaibhav

+0

明確に説明するための質問を編集する –

答えて

0

次のコードを試してみてください。それは問題なく動作します。

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

     UITextRange *selectRange = [textView selectedTextRange]; 
     NSString *selectedText = [textView textInRange:selectRange]; 
    } 
+0

funcのselectRangeを変更したいだけです。 –