2012-12-15 76 views
1

在我的应用程序中,我有UITextView。在该UITextView用户输入文本中,当用户单击UIButton(已在工具栏中)时,它需要在光标当前位置插入UIButton。我怎样才能得到游标的X和Y配对。编程?如何在UITextView UITextView中的任何光标位置插入UIButton

我的代码如下的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

可以使用caretRectForPosition方法找到你的光标点也许你可以向前

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

步骤为iOS5.0及以上 –

+0

看来,这是行不通的。 – Jacky

相关问题