Search code examples
iosobjective-cuitableviewuilabelline-breaks

Detect UILabel line break. Place items into UITableViewCell dynamically


I would like to detect after fill up multiple UILabel included in each UITableViewCell of a UITableView if there is a way to detect if the UILabel has a Line Break. Because of I'd like set its frame dynamically. I need to do it because I have an UILabel in top of another and the UILabel thats below the other has to have the the same distance from the other for each row.

I only found the property numberOfLines of a UILabel and I realized that depending of the device (iPhone 6, 6 Plus and the rest), the width of the UITableViewCell changes. So it affects directly to the UILabel space. I could calculate the length of the UILabel and depending of it, change the frame of the UILabel thats below depending of this length but I dont like this way.

Any help?

Thanks


Solution

  • You don't need to detect the line break to do this. You should use auto layout. You should set a vertical spacing constraint between the bottom of the top label and the top of the bottom label -- that will give them a fixed spacing no matter what size the labels expand to.