0

I have one simple label called lblApple, which has text "This is an apple, apple is very good".

Now when user taps on particular part of UIlabel, for example if user taps "good" in that label , then i want that string "good".

I have added tap gesture and all that stuff , but i can not find any solution for this.

Does any one know about it?

Thanks

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • I think you need to create `UILabel` for each of your string.. and attach UITapGesture alongwith each UILabel, and you get the text easily on tapping the label as they are separately added. – iphonic May 13 '14 at 06:10
  • no my friend, that is not good solution. I have more than 5000 words.so it is not good practice. – NiravPatel May 13 '14 at 06:12
  • To be clear, you could have 5000 words as the text property of a UILabel and you want to receive an action and be able to determine which word exactly was tapped on? – Adam Eberbach May 13 '14 at 06:14
  • @AdamEberbach: yes my friend, I have store those sentences in database and i am displaying in UILabel ,now i want the part of text which is tapped inside that label. – NiravPatel May 13 '14 at 06:18
  • 1
    If you want to just selection, you may look at UITextInput's selectedTextRange property. – Ramaraj T May 13 '14 at 06:30

2 Answers2

0

You can use https://github.com/mattt/TTTAttributedLabel. This provides a functionality to add a link in the label. So you can detect whether the link is tapped or not. You can make good as a link in your example. So if the user taps on good, you can handle the action. You need to customize the style of the link here.

source : Tap Gesture on part of UILabel

Community
  • 1
  • 1
manujmv
  • 6,450
  • 1
  • 22
  • 35
0

//Write a regular expression to match a simple string below expression is to find the urls within a string

 NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"(? i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];

// description is your text and descriptionLabel is your TTTAttributedLabel

Assign the delegate

descriptionLabel.delegate = self;

Find the match against the regular expression

 [regexp enumerateMatchesInString:description options:0 range:NSMakeRange(0, [description length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags __unused, BOOL *stop __unused){

            NSURL *url = [NSURL URLWithString:[description substringWithRange:match.range]];
            [descriptionLabel addLinkToURL:url withRange:match.range];

}];

Here is the OnClick Method:

- (void)attributedLabel:(TTTAttributedLabel *)label
 didSelectLinkWithURL:(NSURL *)url
 {
  NSlog("%@",label.text);
 }
Gyanendra Singh
  • 1,483
  • 12
  • 15
  • Ok! Did you change the Regular expression? The regular expression I have mentioned is to find the URL within a sentence like e.g "you can find this product on www.amazon.com and find more on http://www.google.com" will detect www.amazon.com and http://www.google.com and also make sure that you are assigning the delegate as well. descriptionLabel.delegate = self; – Gyanendra Singh May 13 '14 at 06:50
  • yes i have set delegate ,and also changed Regular Expression, but not working. User can tap anywhere on UILabel, so should i give whole text in Regular Expression? – NiravPatel May 13 '14 at 07:04
  • in **[regexp enumerateMatchesInString:description.... method description** should be the whole text in your case it should be "This is an apple, apple is very good". Well I am not sure then as I was able to detect and perform the Tap action on the links within the sentence. try to put debug check if control is going inside **[regexp enumerateMatchesInString:description....** block or not. – Gyanendra Singh May 13 '14 at 07:07