How can I get the selectedRange.location value?

I want to get the cursor position from a UITextview, I am implementing the code in How to find the pixel-positive cursor in a UITextView? post, the algorithm is based on the algorithm at the Pixel cursor position in a UITextView .

My problem is that I cannot get the selected Range.location value after the keyboard appears a second time, so it always indicates that the location value is 2147483647, which means that it was not found. This is my implementation:

-(void)getCursorPosition{ NSRange originalPosition = self.codeTextView.selectedRange; NSLog(@"original position : %d", self.codeTextView.selectedRange.location); CGPoint origin = self.codeTextView.frame.origin; unichar c = ' '; NSLog(@"location : %d", self.codeTextView.selectedRange.location); NSLog(@"length : %d", self.codeTextView.selectedRange.length); if (self.codeTextView.selectedRange.location != [self.codeTextView.text length]) { // NSLog(@"cek 1"); c = [self.codeTextView.text characterAtIndex:self.codeTextView.selectedRange.location]; // NSLog(@"cek 2"); } NSLog(@"c : %d", c); if (c!=32 && c!=10) { NSRange delimiter = [self.codeTextView.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSLiteralSearch range:NSMakeRange(self.codeTextView.selectedRange.location, [self.codeTextView.text length] - self.codeTextView.selectedRange.location)]; if (delimiter.location == NSNotFound) { delimiter.location = [self.codeTextView.text length]; } self.codeTextView.selectedRange = delimiter; NSLog(@"delimiter length : %d, location : %d", delimiter.length, delimiter.location); } int deviationLocation = self.codeTextView.selectedRange.location - originalPosition.location; NSString *head = [self.codeTextView.text substringToIndex:self.codeTextView.selectedRange.location]; CGSize initialSize = [head sizeWithFont:self.codeTextView.font constrainedToSize:CGSizeMake(self.codeTextView.frame.size.width, self.codeTextView.frame.size.height)]; NSUInteger startOfLine = [head length]; BOOL isFirstLine = NO; NSLog(@"initialize height : %f", initialSize.height); NSLog(@"code height : %f", self.codeTextView.contentSize.height); if (initialSize.height / self.codeTextView.contentSize.height == 1) { isFirstLine = YES; } while (startOfLine > 0 && isFirstLine == NO) { NSRange delimiter = [head rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSBackwardsSearch range:NSMakeRange(0, startOfLine)]; startOfLine = delimiter.location; NSString *tempHead = [head substringToIndex:startOfLine]; CGSize tempHeadSize = [tempHead sizeWithFont:self.codeTextView.font constrainedToSize:CGSizeMake(self.codeTextView.frame.size.width, self.codeTextView.frame.size.width)]; int beforeLine = initialSize.height / self.codeTextView.contentSize.height; int afterLine = tempHeadSize.height / self.codeTextView.contentSize.height; if (beforeLine != afterLine) NSLog(@"break"); break; } NSString *tail; if (isFirstLine == NO) { tail = [head substringFromIndex:(startOfLine + deviationLocation)]; }else { tail = [head substringToIndex:startOfLine - deviationLocation]; } CGSize lineSize = [tail sizeWithFont:self.codeTextView.font forWidth:self.codeTextView.frame.size.width lineBreakMode:UILineBreakModeWordWrap]; cursor = origin; cursor.x += lineSize.width; cursor.y += initialSize.height - lineSize.height; self.codeTextView.selectedRange = originalPosition; [self.codeTextView becomeFirstResponder]; NSLog(@"cursor x : %f, y : %f", cursor.x, cursor.y); 

}

I called this method in the textViewShouldBeginEditing method, this is the implementation:

 -(BOOL)textViewShouldBeginEditing:(UITextView *)textView{ [self getCursorPosition]; self.viewTextViewScroll.contentSize = CGSizeMake(self.codeTextView.frame.size.width, self.codeTextView.contentSize.height+100); CGRect frameCode = self.codeTextView.frame; frameCode.size.height = self.codeTextView.contentSize.height + 103; self.codeTextView.frame = frameCode; CGRect frameLine = self.lineNumberTextView.frame; frameLine.size.height = self.codeTextView.contentSize.height +100; self.lineNumberTextView.frame = frameLine; return YES; 

}

Can someone help me please

UPDATE:

I solved this problem using the delegate method textViewDidChangeSelection, here is the code I made:

 -(void)textViewDidChangeSelection:(UITextView *)textView{ counterAppear++; if (counterAppear == 1) { } else if (counterAppear == 2) { isFistAppear = NO; codeBuilderSelectedRange = textView.selectedRange; [self getCursorPosition]; CGFloat xPos = cursor.x - (0.5*self.view.frame.size.width); if (cursor.x < self.view.frame.size.width) { [[[self.viewCodeTextView subviews] lastObject] setContentOffset:CGPointMake(0, cursor.y) animated:YES]; }else { [[[self.viewCodeTextView subviews] lastObject] setContentOffset:CGPointMake(xPos, cursor.y) animated:YES]; } }else if (isFistAppear == NO) { //kondisi saat keyboard masih appear & user pindah2 kursor codeBuilderSelectedRange = textView.selectedRange; [self getCursorPosition]; NSLog(@"cursor x :%f, y : %f", cursor.x, cursor.y); } 

}

I set BOOL isFirstAppear to viewDidLoad, I set this because after I nslog selected the value of Range.location in textViewDidChangeSelection, it is always called twice, the first value always gives the value not found, and the second gives the correct value, so I decided . I installed CounterAppear in the keyboard method (I made a method that called NSNotification in viewDidLoad). counterAppear is a value that gives me a condition for the difference when I click on the text.

+1
ipad uitextview uiscrollview keyboard cursor-position
source share
1 answer

In textViewDidBeginEditing, the location value will sometimes be incorrect. To fix this, do the following:

 -(void)textViewDidBeginEditing:(UITextView *)textView { [self performSelector:@selector(beginEditing:) withObject:textView afterDelay:0]; }-(void)beginEditing:(UITextView*)sender { //code here } 
+1
source share

All Articles