Search code examples
iphoneiosnsstringuibuttonnsrange

Create UIButton on substring with help of NSRange


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.


Solution

  • -->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.