如何在iPhone的UITextview中获取光标的确切位置。我知道可以通过使用获得光标的位置 'text_view.selectedRange.location'。 我想要光标的X和Y坐标。我怎么能这样做?
答案 0 :(得分: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 包含该点。