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 ...

4

2 回答 2

0

你好堆栈溢出社区。我希望我不要回答太晚。

- (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;
}

然后是 Sunny 建议的代码。

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

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

查看 UITextInput 协议参考以获取更多信息。

于 2013-04-03T00:07:34.497 回答
0

像这样尝试它会在我的情况下工作它正在工作,

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

- (void)setCursorPosition:(UITextView *)textview 
{ 
    textview.selectedRange = NSMakeRange(10, 0); 
} 
于 2013-03-14T07:45:15.913 回答