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
Spróbuj zmienić pos na ** pos = [sender locationInView: _tv] **, a następnie usuń regulację pos. – rmaddy
o, jesteś geniuszem! Dziękuję bardzo!! – KBouldin9
Sprawdź http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger