Find text in array and return index number

user6631314

I am displaying an array of strings separated by \n or linebreaks in a textview. Thanks to the \n, if there is room in the textview, each string gets its own line. However, if the width of the textview is less than the string, then the textview automatically wraps the line to the next line so that the string in effect takes two lines. Nothing wrong with this so far.

However, I want to grab the string if someone touches it. It works fine if the string fits on a line. However, if the string has been broken across two lines by textview, if the user touches the top line, I need to attach the line below to get the whole string. Or if the user touches the bottom line, I need to get and add the previous one to have a whole string.

Can anyone suggest proper way to do this?

Here is my code that grabs the line the person touches, however, it only gets the line touched and fails when it tries to calculate the index of the string in the array.

Thanks for any suggestions:

- (void) handleTap:(UITapGestureRecognizer *)recognizer{
    UITextView *textView =  (UITextView *)recognizer.view;
    CGPoint location = [recognizer locationInView:textView];
    CGPoint position = CGPointMake(location.x, location.y);
     UITextPosition *tapPosition = [textView closestPositionToPoint:position];

    UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityLine inDirection:UITextLayoutDirectionRight];
//In following line, I am not getting the full text in the string, only the text of that line.  I need to get the whole string.
    NSString *tappedLine = [textView textInRange:textRange];
    NSArray* myArray  = [self.listSub.text componentsSeparatedByString:@"\n"];     
      NSInteger indexOfTheObject = [myArray indexOfObject: tappedLine];
//If the tapped line is not the same thing as the string, the above index becomes huge number like 94959494994949494 ie an error, not the index I want.
    }
Mike Taverne

This is indeed possible.

First, you need to change the granularity to UITextGranularityParagraph:

UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityParagraph inDirection:UITextLayoutDirectionRight];

This will return you the entire wrapped line of text that the user tapped, no matter where they tapped it.

However, this text will include the trailing \n character that marks the end of the paragraph. You need to remove this before comparing the text to your array. Replace the last line of your code above with these two lines:

NSString *trimmedLine = [tappedLine stringByTrimmingCharactersInSet:
                              [NSCharacterSet newlineCharacterSet]];
NSInteger indexOfTheObject = [myArray indexOfObject: trimmedLine];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find the 2nd largest number in the array, but return the last index that the value appears in?

From Dev

Javascript: Find 2nd occurrence(last) of largest number & return same index in different array

From Dev

JavaScript return index of minimum and maximum number in array

From Dev

Return value of smallest index of repeated number in array

From Dev

Return index number of matching item in array

From Dev

Return value of smallest index of repeated number in array

From Dev

for each index in array,return smaller number

From Dev

Find the largest number and its index in the random array

From Dev

Find word in part of cell text, return column number: Excel VBA

From Dev

How to find index of array of records return from database in rails?

From Dev

How to find array items that begin with a number and return it in reverse alphabetical order?

From Dev

Java Script Get Number of index for array from text box

From Dev

String array: search for text, return line number of first occurrence

From Dev

Find text and replace with index

From Dev

Numpy return array of index

From Dev

How do I find the unique number in an array and return only that number in ruby?

From Dev

Find multiple index in array

From Dev

Find index of array in ArrayList

From Dev

Find index of array class

From Dev

Index & Match to return text value

From Dev

Find a number in an array

From Dev

Find nested foreach index number

From Dev

Use find to return only the number

From Dev

How can you return the number of times an item is saved to a specific array index?

From Dev

Return the index of an item in an Array in Hive

From Dev

Java recursion, return array index

From Dev

Return index of nearest values in an array

From Dev

Array index does not return the value

From Dev

Excel: Index/match to return array

Related Related

  1. 1

    How to find the 2nd largest number in the array, but return the last index that the value appears in?

  2. 2

    Javascript: Find 2nd occurrence(last) of largest number & return same index in different array

  3. 3

    JavaScript return index of minimum and maximum number in array

  4. 4

    Return value of smallest index of repeated number in array

  5. 5

    Return index number of matching item in array

  6. 6

    Return value of smallest index of repeated number in array

  7. 7

    for each index in array,return smaller number

  8. 8

    Find the largest number and its index in the random array

  9. 9

    Find word in part of cell text, return column number: Excel VBA

  10. 10

    How to find index of array of records return from database in rails?

  11. 11

    How to find array items that begin with a number and return it in reverse alphabetical order?

  12. 12

    Java Script Get Number of index for array from text box

  13. 13

    String array: search for text, return line number of first occurrence

  14. 14

    Find text and replace with index

  15. 15

    Numpy return array of index

  16. 16

    How do I find the unique number in an array and return only that number in ruby?

  17. 17

    Find multiple index in array

  18. 18

    Find index of array in ArrayList

  19. 19

    Find index of array class

  20. 20

    Index & Match to return text value

  21. 21

    Find a number in an array

  22. 22

    Find nested foreach index number

  23. 23

    Use find to return only the number

  24. 24

    How can you return the number of times an item is saved to a specific array index?

  25. 25

    Return the index of an item in an Array in Hive

  26. 26

    Java recursion, return array index

  27. 27

    Return index of nearest values in an array

  28. 28

    Array index does not return the value

  29. 29

    Excel: Index/match to return array

HotTag

Archive