2012-10-17 2 views
0

iPhone의 UITextview에서 커서의 정확한 위치를 얻으려면 어떻게해야합니까? 커서의 위치는 'text_view.selectedRange.location'을 사용하여 알 수 있습니다. 커서의 X 좌표와 Y 좌표를 원합니다. 어떻게해야합니까?iPhone의 uitextview에서 정확한 커서 위치 얻기

+0

시작하는 것이 좋습니다. http : //stackoverflow.com/questions/1117065/cocoa-getting-the-current-mouse-position-on-the-screen – MCKapur

답변

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는 점을 포함.