2011-03-09 75 views
1

好,模拟按键的iPhone

这是一件事,我试图模拟一个按键与按键:我有一个UITextView中,用户可以输入文字像往常一样,触摸按钮。 ..它将不得不出现让我们说在当前光标位置“b”。

它似乎不是很难,但谷歌搜索我什么也没有发现。

回答

2

您可以手动设置的按钮事件方法在UITextView中的文本:

UITextView* yourTextView; 

NSRange selectedRange = yourTextView.selectedRange; 

NSString* currentText = yourTextView.text; 

NSString* yourString = @"b"; // Or any other string of any length 

// Insert the string at the cursor selected range 
NSString* modifiedText = [currentText substringToIndex: range.location]; 
modifiedText = [modifiedText stringByAppendingFormat:@"%@%@", yourString, 
    [currentText substringFromIndex: range.location + range.length]]; 

// Replace the text of the UITextView 
yourTextView.text = modifiedText; 

编辑插入光标所在位置的字符串。通过这种修改,如果您选择文本并点击按钮,您将替换您选择的内容。

编辑:固定编译问题

希望它能帮助。

问候!

+0

事实上,我现在在工作,我没有在这里和我一起工作的项目,只要我回家,我会给你的代码一试,看来它可能会工作:)谢谢! – ferostar 2011-03-09 18:37:45

+0

完成!它工作得很漂亮,有两件事:selectedRange不是一个指针,而是与“+”(在obj-c中不可能)连接,而是用stringByAppendingFormat完成的。 – ferostar 2011-03-10 01:46:59

+0

@seretur:很高兴知道。我解决了你提到的问题。问候! – redent84 2011-03-10 09:41:45