How to set cursor position for UITextView on user input?

I am looking for a simple answer for this problem…

I have a UITextView in which the user can start typing and click on DONE and resign the keyboard.

  • deep mutable copy of a NSMutableDictionary
  • Determine the Device Orientation and its resulting Angle on one Dimension?
  • UIScrollView Custom Paging
  • Index '5' beyond bounds of empty array crash
  • Assertion failure in UITableView configureCellForDisplay:forIndexPath:
  • Insert new row in UITableView when tapping first row
  • When the wants to edit it again, I want the cursor (the blinking line) to be at the first position of the textView, not at the end of textView. (act like a placeholder)

    I tried setSelectedRange with NSMakeRange(0,0) on textViewDidBeginEditing, but it does not work.

    More Info:

    It can be seen that.. when the user taps on the textView the cursor comes up at the position where the user taps on the textView.

    I want it to always blink at starting position when textViewDidBeginEditing.

    6 Solutions Collect From Internet About “How to set cursor position for UITextView on user input?”

    The property selectedRange can not be assigned at “any place”, to make it work you have to implement the method - (void)textViewDidChangeSelection:(UITextView *)textView, in your case:

    - (void)textViewDidChangeSelection:(UITextView *)textView
    {
        [textView setSelectedRange:NSMakeRange(0, 0)];
    }
    

    you will have to detect when the user is beginning editing or selecting text

    My solution:

    - (void) viewDidLoad {
        UITextView *textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 200, 200)];
        textView.text = @"This is a test";
        [self.view addSubview: textView];
        textView.delegate = self;
        [textView release];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(tapped:)];
        [textView addGestureRecognizer: tap];
        [tap release];
    }
    - (void) tapped: (UITapGestureRecognizer *) tap {
        [textView becomeFirstResponder];
    } 
    
    - (void) textViewDidBeginEditing:(UITextView *)textView {
        textView.selectedRange = NSMakeRange(0, 0);
    }
    

    I guess it’s UITextView internal mechanism to set the cursor when user taps on it. We need to override that by attaching a tap gesture recognizer and call becomeFirstResponder instead.

    I was facing the same issue – basically there’s a delay when becoming first responder that doesn’t allow you to change selectedRange in any of textView*BeginEditing: methods. If you try to delay the setSelectedRange: (let’s say with performSelector:withObject:afterDelay:) it shows ugly jerk.

    The solution is actually pretty simple – checking order of delegate methods gives you the hint:

    1. textViewShouldBeginEditing:
    2. textViewDidBeginEditing:
    3. textViewDidChangeSelection:

    Setting selectedRange in the last method (3) does the trick, you just need to make sure you reposition the cursor only for the first time when the UITextView becomes first responder as the method (3) is called every time you update the content.

    A BOOL variable set in shouldChangeTextInRange: one of the methods (1), (2) and check for the variable in (3) should do the trick … just don’t forget to reset the variable after the reposition to avoid constant cursor reset :).

    Hope it helps!

    EDIT

    After few rounds of testing I decided to set the BOOL flag in shouldChangeTextInRange: instead of (2) or (3) as it proved to be more versatile. See my code:

    @interface MyClass
    {
        /** A flag to determine whether caret should be positioned (YES - don't position caret; NO - move caret to beginning). */
        BOOL _isContentGenerated;
    }
    
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
        // deleting
        if([text length] == 0)
        {
            // deleting last character
            if(range.length == [[textView text] length])
            {
                // reached beginning
                /** 
                 code to show placeholder and reset caret to the beginning 
                */
                _isContentGenerated = NO;
            }
        }
        else
        {
            // adding
            if(range.location == 0)
            {
                /** 
                 code to hide placeholder
                */
                _isContentGenerated = YES;
            }
        }
        return YES;
    }
    
    - (void)textViewDidChangeSelection:(UITextView *)textView
    {
        if(!_isContentGenerated)
        {
            [textView setSelectedRange:NSMakeRange(0, 0)];
        }
    }
    

    I haven’t worked enough with that to help you fully, but what happens when you try to play with different selectedRanges? Say, if you do [... setSelectedRange:[NSMakeRange(0,1)]] or [... setSelectedRange:[NSMakeRange(1,0)]]? Does it move the cursor anywhere?

    So I ended up adding a UILabel over the UITextView which acts as a placeholder for the textView. Tapping on the UILabel would send the action down to the textView and becomeFirstResponder. Once you start typing, make the label hidden.

    [_detailAreaView setTextContainerInset:UIEdgeInsetsMake(8, 11, 8, 11)];