lost
lost

Reputation: 111

Set cursor to the beginning inside UITextView

textView.selectedRange = NSMakeRange(0, 0);

This has no effect on where the cursor is placed.

I then used a breakpoint and typed in po textView.selectedRange in the debugger.

The result was:

(lldb) property 'selectedRange' not found on object of type 'UITextView *'

Upvotes: 1

Views: 597

Answers (1)

Vignesh Kumar
Vignesh Kumar

Reputation: 598

Since UITextView inherits from UIResponder. So, you can call the -becomeFirstResponder method on your text view, which will cause it to become the first responder and begin editing:

    [textView becomeFirstResponder];

After that, you can selectedRange of UITextView.

    [textView setSelectedRange:NSMakeRange(0, 10)];

Steps

  1. Added the UITextView in Story board view controller.
  2. Added the UITextView delegate in View controller.

    @interface SecondViewController : UIViewController<UITextViewDelegate>
    
  3. Assign the property to text view.

    @property (strong, nonatomic) IBOutlet UITextView *textView;
    
  4. And selected the text range in textview.

    - (void)viewDidLoad {
          [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
    
            [self.textView becomeFirstResponder];
            [self.textView setSelectedRange:NSMakeRange(0, 10)];
    }
    

Upvotes: 1

Related Questions