2013-03-14 168 views
1

我想根據文本視圖上的x,y座標設置光標位置。例如,我想要在(10,5,300,462)上的光標位置,我想僅基於光標插入此幀的文本。在textview上設置光標位置

鑑於沒有負載我設置委託,並使用NSMakeRange()函數

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

我創建的(10,22,300,462)幀文本視圖,並添加到視圖。我的要求是我希望顯示光標的文本基礎上的x,y座標x = 10之後和y = 5在文本視圖後...

+0

請參閱:http://stackoverflow.com/questions/10135086/how-to-set-cursor-position-for-uitextview-on-user-input – Vishal 2013-03-14 06:50:40

+0

http://stackoverflow.com/questions/8891072/how如何設置光標在uitextview – Rushabh 2013-03-14 06:50:53

+0

你是否得到了解決方案? – Balu 2013-03-14 07:06:40

回答

0

嘗試像這樣它在我的情況下工作,它的工作,

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

- (void)setCursorPosition:(UITextView *)textview 
{ 
    textview.selectedRange = NSMakeRange(10, 0); 
} 
+0

晴天我想工作光標的特定邊界... – user1997077 2013-03-14 12:13:42

+0

例如textview的大小是(0,0,300,421),我想光標位置(10,5.250,400)..剩餘區域光標不起作用... – user1997077 2013-03-14 12:14:29

0

hello stack overflow community。 我希望我不會遲到。

- (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協議參考以獲取更多信息。