如何在UITextView中的任何光标位置的UITextView中插入UIButton

时间:2012-12-15 07:03:38

标签: iphone objective-c cursor position uitextview

在我的应用程序中,我有一个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值。?

1 个答案:

答案 0 :(得分:1)

您可以使用caretRectForPosition方法查找光标点,然后您可以前进

CGPoint cursorPosition = [textview caretRectForPosition:textview.selectedTextRange.start].origin;