Get word from long tap in a word of UITextView
Asked Answered
C

2

5

Now I already detect long tap in 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);
    }

Now, How should I do to get content of word which got long press, and get a rect of that word to prepare to show the PopOver?

Cordalia answered 5/7, 2012 at 17:29 Comment(1)
Please check #8812409 . I have used UITapGestureRecognizer you can replace it with UILongPressGestureRecognizer.Erlin
S
15

This function will return the word at a given position in an 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];
}
Saguaro answered 11/7, 2012 at 21:29 Comment(4)
You might want to check out the rangeEnclosingPosition:withGranularity:inDirection: method of the text view's tokenizer property.Reifel
Thank you rob, that makes it much simpler! I've edited the answer to include your suggestion.Saguaro
Is there a way to do this backwards like get position of a word?Protagoras
@TheDeveloper The UITextInput protocol provides a function i use if i want to know the UITextPosition of a specific offset (i.e. the char location in a string) of an UITextView: positionFromPosition:(UITextPosition *)startPosition offset:(NSInteger)offset. The startPosition could be tv.beginningOfDocument (if your Textview is named 'tv') and the offset the location of the word in the string assigned as text to the textview. To get the layout point at the returned position use the origin of the rect from caretRectForPosition:Saguaro
E
0

SWIFT 4

A copy of @cayeric's answer written in swift for your convenience.

func getWord(at position: CGPoint, in textView: UITextView) -> String?{
    var point = position

    //eliminate scroll offset
    point.y += textView.contentOffset.y

    //get location in text from textposition at point
    guard let tapPos = textView.closestPosition(to: point) else {
        return nil
    }

    //fetch the word at this position (or nil, if not available)
    guard let wordRange = textView.tokenizer.rangeEnclosingPosition(tapPos, with: .word, inDirection: UITextWritingDirection.rightToLeft.rawValue) else {
        return nil
    }

    return textView.text(in: wordRange)
}
Eggert answered 18/5, 2018 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.