1

I want to to set the cursor position based on the x,y coordinates on the text view. For example I want cursor position on (10,5,300,462), I want to insert the text this frame only based on cursor.

In view did load i set the delegate and use the NSMakeRange() function

- (void)viewDidLoad
    {
        [super viewDidLoad];
         _TextView.delegate=self;
           _TextView.editable = YES;
          [_TextView setSelectedRange:NSMakeRange(5,0)];
    }

I create a text view with frame of (10,22,300,462) and added to the view. My reqirement is i want display the cursor on text base on x,y coordinates x=10 after and y=5 after in the text view ...

danielbeard
  • 8,805
  • 3
  • 40
  • 58
user1997077
  • 119
  • 1
  • 3
  • 11

2 Answers2

0

try like this it'l work in my case it's working,

- (void)textViewDidBeginEditing:(UITextView *)textview 
{ 
    [self performSelector:@selector(setCursorPosition:) withObject:textview afterDelay:0.01]; 
} 

- (void)setCursorPosition:(UITextView *)textview 
{ 
    textview.selectedRange = NSMakeRange(10, 0); 
} 
Balu
  • 8,440
  • 2
  • 20
  • 41
  • for example textview size is(0,0,300,421) and i want cursor position on (10,5.250,400)..remaining area cursor not works... – user1997077 Mar 14 '13 at 12:14
0

hello stack overflow community. i hope i don't answer to late.

- (NSUInteger) textOffsetFromPoint: (CGPoint *) position
{
    // this gives you the nearest UITextPosition
    UITextPosition * closestPosition = [textView closestPositionToPoint:tapPoint];

    // and this "translate" UITextPosition into an integer
    NSUInteger offset = [textView offsetFromPosition:textView.beginningOfDocument 
                                          toPosition:closestPosition];
    return offset;
}

then the code suggested by Sunny.

[self performSelector:@selector(setCursorPosition:) withObject:textView afterDelay:0.01];

- (void)setCursorPosition:(UITextView *)textview
{
    textview.selectedRange = NSMakeRange([self textOffsetFromPoint:position], 0);
}

Look UITextInput Protocol Reference for more information.

Leg1oneR
  • 311
  • 2
  • 2