2012-10-14 13 views
12

में टैप से शब्द प्राप्त करें मैं एक टैप से एक शब्द प्राप्त करने का प्रयास कर रहा हूं। निम्नलिखित कार्य sender से UITextView, वाक्य पर है।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]); 
} 

यह है कि मैं क्या पहले से ही है, लेकिन यह शून्य

के रूप में शब्द बाहर प्रिंट
+8

** pos = [प्रेषक स्थान INView: _tv] ** को pos बदलने का प्रयास करें और फिर pos.y समायोजन को हटा दें। – rmaddy

+0

ओह आप एक प्रतिभाशाली हैं! बहुत बहुत धन्यवाद!! – KBouldin9

+0

कृपया जांचें http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger

उत्तर

10

"स्थिति = करने के लिए स्थिति बदलने का प्रयास करें [इस locationInView: _tv]। और फिर pos.y समायोजन निकालने - rmaddy "

+0

यह तब तक अच्छा काम करता है जब तक UITextview स्क्रॉल नहीं किया जाता है। लेकिन जब हम टेक्स्टव्यू स्क्रॉल करते हैं, तो सही शब्द मुद्रित नहीं होता है। –