Setting the cursor position UITextView to the end of the text

I have been looking for a solution for this all morning, but have not yet found something that works.

I have a text view in which there is a certain fixed text that I do not want the user to be able to change. In this case, each of my textual representations begins with "1.", "2." etc. The idea is that the entered text will be numbered for what I do later.

I do not want the user to be able to delete this text (it is essentially "persistent"). I also do not want to let them start adding text in the middle of this preliminary text.

To handle this, I did:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if (range.location < 3) return NO; return YES; } 

This works great, except if the user touches somewhere in the parts "1.", "2." etc., it will set the cursor there, which then prevents the user from entering text due to checking the location of the range. What I want to do in this case is to set the cursor (possibly in textViewDidBeginEditing) to the end of the text in the view. However, no matter what combination of selectedRange I use, I just can't get the rough cursor to move to the end. Any help would be greatly appreciated.

+6
source share
5 answers

You can consider registering with UIKeyboardWillShowNotification and after receiving the notification, set textview userInteractionEnabled to NO .

In addition, implement the shouldChangeTextInRange method so that if replacementText is equal to the string @"" , you do not change the text ( @"" means that the user presses backspace). Restore user interaction when the user finishes editing the text and there you go.

Good luck

+4
source

To move the cursor to the end

 textView.selectedRange = NSMakeRange([textView.text length], 0); 

or to move the cursor after the third character

 textView.selectedRange = NSMakeRange(3, 0); 

Another, maybe the best approach, is to clear the first three characters when the user starts editing, and then adds them back after editing is complete.

+10
source

Late, but I found a working solution for this on some blog. he needs a little hack

 - (void) textViewDidBeginEditing:(UITextView*)textview { [self performSelector:@selector(placeCursorAtEnd:) withObject:textview afterDelay:0.01]; } - (void)placeCursorAtEnd:(UITextView *)textview { NSUInteger length = textview.text.length; textview.selectedRange = NSMakeRange(length, 0); } 
+6
source

I tried to create a currency field. I did not want the user to click across the field and possibly delete the formatted text. The easiest way to find the cursor at the end is to do the following in a subclass of UITextField

 -(void) setSelectedTextRange:(UITextRange *)selectedTextRange{ [super setSelectedTextRange:selectedTextRange]; self.text = self.text; } 
+3
source

This method hides auto-correction, but the keyboard does not jump.

 - (void)rejectAutoCorrectSuggestionInTextView:(UITextView *)textView { if ([textView isFirstResponder]) { NSString *original = textView.text; NSRange originalRange = textView.selectedRange; CGPoint originalOffset = textView.contentOffset; [UIView animateWithDuration:.00001 animations:^{ textView.text = [textView.text stringByAppendingString:@"|"]; NSRange range; range.location = textView.text.length-1; range.length = 0; textView.selectedRange = range; textView.text = [textView.text substringToIndex:textView.text.length - 1]; }]; NSString *final = textView.text; if (![original isEqualToString:final]) { textView.text = original; textView.selectedRange = originalRange; [textView setContentOffset:originalOffset animated:NO]; } } } 
0
source

Source: https://habr.com/ru/post/916681/


All Articles