1

在我的应用程序中,我有一个UITextView. 在该UITextView用户输入文本时,当用户单击 UIButton(已在工具栏中)时,需要UIButton在光标当前位置插入一个。我怎样才能得到光标的 X 和 Y 坐标。以编程方式?

我的代码在UIButton 下面(已经在工具栏中)

-(void)inserButton
{
    UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeCustom];
    myButton.frame      =   CGRectMake(30, 5, 10, 10);
    UIImage *Chkimg = [UIImage imageNamed:@"unchk.png"];
    [myButton setBackgroundImage:Chkimg forState:UIControlStateNormal];
    [note addSubview:myButton];
}

在这里我需要替换值

myButton.frame = CGRectMake(X, Y, 10, 10);

是否可以获取光标的 X 和 Y 值。?

4

1 回答 1

1

你可以使用caretRectForPosition方法来找到你的光标点也许然后你可以向前迈进

CGPoint cursorPosition = [textview caretRectForPosition:textview.selectedTextRange.start].origin;
于 2012-12-15T09:32:18.250 回答