2012-07-05 22 views
5
का एक शब्द में लंबे नल से बात पहुंचाएं

अब मैं पहले से ही लंबी नल UITextView मेंUITextView

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     UILongPressGestureRecognizer *LongPressgesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFrom:)];  
     [[self textview] addGestureRecognizer:LongPressgesture]; 
     longPressGestureRecognizer.delegate = self; 
    } 
    - (void) handleLongPressFrom: (UISwipeGestureRecognizer *)recognizer 
    { 
     CGPoint location = [recognizer locationInView:self.view]; 

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

अब पता लगा, मैं कैसे शब्द जो लंबे समय से प्रेस मिल गया की सामग्री मिलता है, और इस बात का एक रेक्ट पाने के लिए क्या करना चाहिए PopOver दिखाने के लिए तैयार करने के लिए शब्द?

+0

कृपया जांच करें http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829। मैंने 'UITapGestureRecognizer 'का उपयोग किया है, जिसे आप इसे' UILongPressGestureRecognizer 'से बदल सकते हैं। – TheTiger

उत्तर

15

यह फ़ंक्शन UITextView में किसी दिए गए स्थान पर शब्द वापस कर देगा।

+(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv 
{ 
    //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]; 

    return [_tv textInRange:wr]; 
} 
+1

आप टेक्स्ट रेंज की 'टोकननाइज़र' प्रॉपर्टी के 'रेंज एन्क्लोज़िंग पोशन: ग्रैन्युलरिटी: इनडिरेक्शन:' विधि को देखना चाहेंगे। –

+0

धन्यवाद लूट, यह बहुत आसान बनाता है! मैंने आपके सुझाव को शामिल करने के लिए उत्तर संपादित किया है। – cayeric

+0

क्या किसी शब्द की स्थिति पाने के पीछे ऐसा करने का कोई तरीका है? – Sirens