UITextView 更改 selectRange 总是崩溃
Posted
技术标签:
【中文标题】UITextView 更改 selectRange 总是崩溃【英文标题】:UITextView change selectRange always crash 【发布时间】:2016-09-22 09:38:31 【问题描述】:当用户单击textView中的段落时,我想将光标更改为linebreak
,但是当我更改selectionRange
时总是失败。
我知道原因,但我必须更改func: textViewDidChangeSelection:(UITextView*)textView
中的selectedRange
如何更改 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
*/
对不起我的英语不好。
【问题讨论】:
是“更改 selectionRange 总是失败”还是 selectedRange ..? 我编辑问题解释清楚 【参考方案1】:您可以尝试以下代码,它对我来说很好。
- (void)textViewDidChangeSelection:(UITextView *)textView
UITextRange *selectRange = [textView selectedTextRange];
NSString *selectedText = [textView textInRange:selectRange];
【讨论】:
我只是想改变 func 中的 selectRange ,而不是这个以上是关于UITextView 更改 selectRange 总是崩溃的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中更改 UITextView 的 textColor