0

I have some text coming from server. It may be single line or multiline text. I have to display the text on UILabel, which is no problem for me. The problem is, I have to display UIButton on finding a particular substring of the same text. For example the text is Nitish\n435-234-6543\nIndia which is being displayed as follows :

Nitish  
435-234-6543  
India  

So, when I find 435-234-6543 I have to display UIButton on 435-234-6543.

Notes:

  1. The text is dynamic - coming from server. Above is only an example.
  2. UIButton will be a subview of UILabel.

I tried different ways like OHAttributedLabel, rectForLetterAtIndex and this too. But not getting success. What my idea is, to create the button when substring is found and to set the frame of button based on NSRange of substring. Is this a possibility? How can it be done? Or is there some other way to do this?

I guess it is the approach I am worried about.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • Would you like to elaborate your issue? I am not clear about your issue. Are you not getting correct rect or issue in adding button inside label. – Iducool Jan 14 '12 at 09:05
  • Getting the rect for the button where the substring is located is the problem for me. – Nitish Jan 14 '12 at 09:07

3 Answers3

0

For those who want perfect solution here's the solution on how to get CGRect of a substring.

Community
  • 1
  • 1
Islam
  • 3,654
  • 3
  • 30
  • 40
0

1) Easy solution:

Make your UILabel a UITextView and use the property dataDetectorTypes to have phone numbers as links automatically.


2) More involved solution:

There is a convenient method to determine the size any text will need to be drawn:

CGSize size = [label.text sizeWithFont:label.font 
                     constrainedToSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX) 
                         lineBreakMode:UILineBreakModeWordWrap];

You could now determine which field is the phone number by splitting the string into its lines with:

NSArray *comp = [label.text componentsSeparatedByString:@"\n"];

and then checking which one is numeric. Now you would have to calculate the exact frame from the height of your size variable, maybe like this:

CGFloat positionOfNumber; // the index of your line in comp cast to CGFloat
CGFloat buffer = 10; // fiddle with this
CGFloat buttonHeight = (size.height- 2*buffer)/[comp length];
CGFloat buttonY = buffer + positionOfNumber * buttonHeight;
CGRect buttonFrame = CGRectMake(0, buttonY, label.frame.size.width, buttonHeight); 
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • The above string is only an example. Phone number and address can be on the same. I have no difficulty in getting the phone number with NSDataDetector. It also works with UILabel. – Nitish Jan 14 '12 at 09:47
  • So you can insert your own `\n` and use the same logic with any data type. – Mundi Jan 14 '12 at 10:56
0

-->I have tried to calcluate position but didnt get success. Seems lots of work to calcualate position. One immediate solution come to my mind is why are you not taking one or two labels and one button.

Suppose. You got dynamic string from webservice is: "Nitesh-56789-Test".

  1. Find range of string suppose i.e. "56789".
  2. String before starting location of that range assign to one label i.e. assign "Nitesh" to lable one.
  3. Now add one custom button with our searched string as a text(56789).
  4. Now make sure in main string there something after our substring or not. Here I mean after our search string "56789" still "Test" remain so assign it to third lable.


Here you have to figue out frame of all labels and button using dynamic height width calculation by using sizeWithFont method.

Iducool
  • 3,543
  • 2
  • 24
  • 45
  • There are four things I need to identify : phone number, email, mail address and web address. This means I have to take couple of labels as you said managing whom will be not easy. Also as the string is coming from server, it may or may not contain above information. In that case it will be very difficult to manage the frames of different labels. – Nitish Jan 14 '12 at 10:36
  • All your four requirement will be satisfied with UITextView.You can go with it – Iducool Jan 14 '12 at 10:53