Jason
Jason

Reputation: 5307

Setting UITextView cursor position to end of text

I've looked for a solution to this all morning, but have yet to find something that works.

I have a text view that has some existing fixed text in it that I don't want the user to be able to modify. In this case, each of my text views start with "1. ", "2. ", etc. The idea being that the text they entered will be numbered for something I'm doing later.

I don't want the user to be able to delete this text (it is essentially "permanent"). I also don't want to allow them to start adding text in the middle of this pre-text.

To handle this, I have done:

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

    return YES;
}

This works great, except that if the user touches anywhere in my "1. ", "2. ", etc. parts of the view, it will set the cursor there, which then prevents the user from typing text because of the range location check. What I want to do in this case is set the cursor (perhaps in textViewDidBeginEditing) to the end of the text in the view. However, regardless of what combination of selectedRange I use, I just can't get the darn cursor to move to the end. Any help would be greatly appreciated.

Upvotes: 13

Views: 23782

Answers (6)

dr OX
dr OX

Reputation: 4739

extension UITextView {
    func moveCoursoreToEnd(){
        selectedTextRange = textRange(from:endOfDocument, to: endOfDocument)
    }
}

Upvotes: 2

Shaik Riyaz
Shaik Riyaz

Reputation: 11462

It's late but i found working solution for this in some blog . it 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);
}

Upvotes: 10

Nuzhdin Vladimir
Nuzhdin Vladimir

Reputation: 1822

This method hide autocorrection & keyboard 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];
        }
    }
}

Upvotes: 1

datinc
datinc

Reputation: 3552

I was trying to make a currency field. I did not want the user pressing all over the field and potentially deleting formatted text. The easiest way I found to keep 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;
}

Upvotes: 3

Stavash
Stavash

Reputation: 14304

You could consider registering to the UIKeyboardWillShowNotification and upon receiving the notification, set the textview's userInteractionEnabled to NO.

Also, implement the shouldChangeTextInRange method in a way that if replacementText is equal to the string @"" you don't change the text (@"" meaning the user is tapping backspace). Restore user interaction when the user finishes editing the text and there you go.

Good luck!

Upvotes: 5

trapper
trapper

Reputation: 11993

To move the cursor to the end

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

or to move the cursor to after the third character

textView.selectedRange = NSMakeRange(3, 0);

Another, maybe better, approach might be to clear the first three characters out when the user starts editing, then add them back in once editing is over.

Upvotes: 16

Related Questions