在iPhone上的UITextView中获取光标位置? [英] Getting cursor position in a UITextView on the iPhone?
本文介绍了在iPhone上的UITextView中获取光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们的iPhone应用程序中有一个可编辑的UITextView。当用户按下某些工具栏按钮时,我们需要在光标位置插入一些文本,但似乎无法找到找到光标当前位置的文档化(或未记录的)方法。
We have a UITextView in our iPhone app which is editable. We need to insert some text at the cursor location when the users presses some toolbar buttons but can't seem to find a documented (or undocumented) method of finding the current location of the cursor.
有没有人有任何想法或有其他人有类似的东西?
Does anybody have any ideas or has anybody else achieved anything similar?
推荐答案
像drewh说的,你可以使用UITextView的selectedRange返回插入点。该范围的长度始终为零。下面的示例显示了如何使用它。
Like drewh said, you can use UITextView's selectedRange to return the insertion point. The length of this range is always zero. The example below shows how to it.
NSString *contentsToAdd = @"some string";
NSRange cursorPosition = [tf selectedRange];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[tf text]];
[tfContent insertString:contentsToAdd atIndex:cursorPosition.location];
[theTextField setText:tfContent];
[tfContent release];
这篇关于在iPhone上的UITextView中获取光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文