How to append a UILabel to the end of text in another UILabel?

1k views Asked by At

In my project, there's a UILabel in each UITableViewCell. Text in each label varies from 1 line to 2 or 3 lines. (I get each text dynamically.) I wonder how I can append another UILabel to the end of each text in UILabel.

I found this Q&A but the author didn't mention solution specifically. Please let me share your ways and problem-solving. Thank you guys in advance!^^

I think I should add some information more. For example, these are 2 labels.


This is the test Label for put in UITableView,

UITableViewCell 01/20



This is another label 01/19


Those dates(01/20, 01/19) that you can see next to each text are the another labels I wanna append. I can't append dates as a string directly cause the normal text and date are have different color and style. I tried 'sizeToFit' as some people told me, but that only show me a frame around whole text. What should I do T_T

2

There are 2 answers

2
Ihor Shubin On

As a variant:

UILabel *someLabel = ...
[someLabel setText:...]
[someLabel sizeToFit]

And then calculate the coordinates of the new insert UILabel.

1
Krrish On
[firstLabel sizeToFit];
CGRect frameForSecondLabel = CGRectMake(firstLabel.frame.origin.x+firstLabel.frame.size.width, firstLabel.frame.origin.y,width,height);
UILabel *secondLabel = [[UILabel alloc] initWithFrame:frameForSecondLabel];

I hope this is what you are looking for