2012-10-17 4 views
0

Как я могу получить точную позицию курсора в UITextview в iPhone. Я знаю, что расположение курсора можно получить, используя 'text_view.selectedRange.location'. Я хочу координаты X и Y курсора. Как я могу это сделать?Получение точного местоположения курсора в uitextview в iPhone

+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 содержит точку.

Смежные вопросы