简体   繁体   中英

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.

对于那些想要完美解决方案的人,这里是如何获取子字符串的 CGRect 的解决方案

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); 

-->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 ie "56789".
  2. String before starting location of that range assign to one label ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM