2012-10-14 15 views
12

Próbuję uzyskać słowo z kranu. Następująca funkcja: sender pochodzi od UIGesture w zdaniu UITextView.Uzyskaj słowo z kranu w UITextView

- (IBAction)printWordSelected:(id)sender { 
    NSLog(@"Clicked"); 

    CGPoint pos = [sender locationInView:self.view]; 
    UITextView *_tv = sentence; 

    NSLog(@"Tap Gesture Coordinates: %.2f %.2f", pos.x, pos.y); 

    //eliminate scroll offset 
    pos.y += _tv.contentOffset.y; 

    //get location in text from textposition at point 
    UITextPosition *tapPos = [_tv closestPositionToPoint:pos]; 

    //fetch the word at this position (or nil, if not available) 
    UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight]; 

    NSLog(@"WORD: %@", [_tv textInRange:wr]); 
} 

to co już mam, ale wypisuje słowo NULL

+8

Spróbuj zmienić pos na ** pos = [sender locationInView: _tv] **, a następnie usuń regulację pos. – rmaddy

+0

o, jesteś geniuszem! Dziękuję bardzo!! – KBouldin9

+0

Sprawdź http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger

Odpowiedz

10

„Spróbuj zmienić pos do pos = [nadawcy locationInView: _tv]. A następnie wyjąć regulację pos.y - rmaddy "

+0

Działa to dobrze, chyba że widok UITextview nie jest przewijany. Ale kiedy przewijamy widok tekstowy, poprawne słowo nie jest drukowane. –

Powiązane problemy