有没有办法在 UITextView 中获取光标(闪烁条)的位置(CGPoint)(相对于其内容最好)。我不是说位置是 NSRange。我需要一些东西:
- (CGPoint)cursorPosition;
应该是非私有(private)API方式。
最佳答案
需要 iOS 5
CGPoint cursorPosition = [textview caretRectForPosition:textview.selectedTextRange.start].origin;
记得在调用此方法之前检查selectedTextRange
不是nil
。您还应该使用 selectedTextRange.empty
来检查它是光标位置而不是文本范围的开头。所以:
if (textview.selectedTextRange.empty) {
// get cursor position and do stuff ...
}
关于iphone - UITextView 中光标的像素位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2633379/