2016-09-22 5 views
0

사용자가 textView에서 항목을 클릭하면 커서가 linebreak으로 변경되지만 변경하면 selectionRange은 항상 실패합니다. 그 이유는 알지만, selectedRangefunc: textViewDidChangeSelection:(UITextView*)textView으로 변경해야합니다.UITextView change selectRange always crash

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 ** is always failed"또는 selectedRange ..입니다. – vaibhav

+0

명확하게 설명 할 질문을 편집하십시오. –

답변

0

다음 코드를 사용해보세요. 정상적으로 작동합니다.

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

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

func에서 selectRange를 변경하고 싶습니다. –