Question

I want to recognize the swipe gesture on the Text presented on the UItextview.

If I have "Enter your text" in the UITextView

I want to recognise the swipe for "Enter your text" and also I want to recognise it separately for each word like

"Enter" If i do swipe on text Enter

"your" If i do swipe on text your

I didn't find anything related in internet

Was it helpful?

Solution

You should be able to do this. UITextView has a layoutManager property which gives you lots of extra access to the string metrics.

When you recognise a swipe on the UITextView get the location of the touch in the view:

- (void)swipe:(UISwipeGestureRecogniser *)recogniser {
    CGPoint point = [recogniser locationInView:textView];
}

After you have the point, you can ask the layout manager for the index in the string where that point is. Check out the docs on NSLayoutManager... Perhaps try:

int index = [textView.layoutManager characterIndexForPoint:point inTextContainer:textView.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];

The you can use some basic string manipulation on the string in the text view to determine the word that that index is at...

Let me know if you need any more help on this, have spent a lot of time wrestling with CoreText and text layouts! :)

Edit: Here's a view controller implementation of this working:

@implementation MyViewController

#pragma mark - View Lifecycle Methods

- (void)viewDidLoad {
    [super viewDidLoad];
    // Create a swipe gesture recogniser
    UISwipeGestureRecognizer *recogniser = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    recogniser.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight; // add other directions if needed
    // Add the swipe gesture recogniser to the text view
    [self.textView addGestureRecognizer:recogniser];
}

#pragma mark - Action Methods

- (void)swipe:(UISwipeGestureRecognizer *)recogniser {
    // Get the location of the gesture recogniser in the text view
    CGPoint point = [recogniser locationInView:self.textView];
    // Get the character index of the swipe
    NSUInteger index = [self.textView.layoutManager characterIndexForPoint:point inTextContainer:self.textView.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];
    // Get the word at the giver index
    NSString *word = [self getWordAtIndex:index fromString:self.textView.text];
    NSLog(@"Swiped on word: %@", word);
}

#pragma mark - Helper Methods

- (NSString *)getWordAtIndex:(NSUInteger)index fromString:(NSString *)string {
    __block NSString *word = nil;
    [string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    if (NSLocationInRange(index, enclosingRange)) {
        word = substring;
        *stop = YES;
    }}];
    return word;
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top