如何在 iPhone 的 UITextview 中获取光标的确切位置。我知道光标的位置可以通过使用'text_view.selectedRange.location'来获得。我想要光标的 X 和 Y 坐标。我怎样才能做到这一点?
问问题
1231 次
1 回答
0
CGPoint origin = myTextView.frame.origin;
NSString* myHead = [myTextView.text substringToIndex:textView.selectedRange.location];
CGSize initialSize = [myHead sizeWithFont:myTextView.font constrainedToSize:myTextView.contentSize];
NSUInteger startOfLine = [myHead length];
while (startOfLine > 0) {
/*
* 1. Adjust startOfLine to the beginning of the first word before startOfLine
* 2. Check if drawing the substring of head up to startOfLine causes a reduction in height compared to initialSize.
* 3. If so, then you've identified the start of the line containing the cursor, otherwise keep going.
*/
}
NSString* textTailSection = [head substringFromIndex:startOfLine];
CGSize lineTotalSize = [textTailSection sizeWithFont:myTextView.font forWidth:myTextView.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
CGPoint cursorPoint = origin;
cursorPoint.x += lineTotalSize.width;
cursorPoint.y += initialSize.height - lineTotalSize.height;
cursorPoint包含该点。
于 2012-10-17T10:38:25.827 回答