3

I have two UILabels in a custom table cell, each with "Lines" set to "0" in Interface Builder. They are vertically stacked with left and right edges aligned, inside a table with row height determined by auto layout. But one of them insists on truncating anyway. Why? It should take up needed lines and push out the height of the table cell.

enter image description here

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • Can I suggest that you include the code you're using in your question? It's difficult to offer help without some idea of the code that is yielding this result. – SmileBot Apr 18 '15 at 01:50
  • was there ever an accepted answer that covered all the issues? – noobsmcgoobs Oct 02 '15 at 03:42

4 Answers4

5

You need to make sure you set the preferredMaxLayoutWidth of the UILabel. iOS8 is supposed to do this for you but I've also had issues with the end being truncated when having custom dynamic sized UITableViewCells. A workaround for this is to set preferredMaxLayoutWidth, in layoutSubviews, to the width of the label.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Okay, that just worked, but I used a hard coded 300, or the cell width. In more complex layout... am I stuck doing the calculations that auto-layout should be doing, to compute this value for `preferredMaxLayoutWidth`? – Rob N Mar 31 '15 at 16:24
  • I was looking at it a bit more and you can set the preferredMaxLayoutWidth in layoutSubviews. I'll add it to my answer. – ABakerSmith Mar 31 '15 at 16:28
  • `layoutSubviews` of which class? I put it in `viewDidLayoutSubviews` of the controller, but the label's width is wrong there - it's 574, but the entire screen is only 320. – Rob N Mar 31 '15 at 16:46
  • Try putting it in your subclass of UITableViewCell. – ABakerSmith Mar 31 '15 at 16:47
  • Same problem. I've got a dev support request open with Apple. I'll post back if they have a solution. – Rob N Mar 31 '15 at 16:54
  • In case you haven't found a solution to this problem: if you create a constraint that defines the height of the label to be, say, above 20 points; the `preferredMaxLayoutWidth` gets set automatically. – ABakerSmith Apr 24 '15 at 08:00
2

Try calling [cell.textLabel sizeToFit] It will resize the labelview according to its content.

Akhil
  • 6,667
  • 4
  • 31
  • 61
  • I put that in a couple places, like `viewDidLayoutSubviews`. I can get it to display the second line, but the cell doesn't grow in height as it should. So the second line of the second label is overlapping into the next cell. – Rob N Mar 30 '15 at 20:22
0

To get automatic cell sizing you need to make sure you have the following (Considering you're using Auto Layout)

  1. Make sure your views have constraints that eventually touch the edges of the cell, in your case, since you just have one label, you can put the top, left, right and bottom spacing to 0 to all edges.
  2. Make sure you set tableView's estimatedRowHeight property to a value, it could be tableView.estimatedRowHeight = 44 for example
  3. Make sure you set tableView's rowHeight property to UITableViewAutomaticDimension on viewDidLoad
  4. Make sure your label has 0 as number of lines

With this, your cells will grow as needed and you should be able to see your labels correctly.

Thanks

batkru
  • 285
  • 2
  • 4
  • Yes, I did all these things. – Rob N Mar 31 '15 at 16:12
  • 2
    Try calling `cell.layoutIfNeeded()` before returning the cell in your implementation of the `func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell` method in the tableview datasource. – batkru Apr 08 '15 at 12:35
0

layoutSubviews for cell and viewDidLayoutSubviews don't work. You needs using custom container view for cell and set preferredMaxLayoutWidth in container layoutSubviews. I have special subclass for UIView

class ExtendedView:UIView{

var didLayoutSubviews:(()->Void)!;

override func layoutSubviews() {
    super.layoutSubviews();
    if let completion = didLayoutSubviews {
        completion();
        layoutIfNeeded();
    }
}

}

using example

container.didLayoutSubviews = {[unowned self] in
    let left =  self.statusLabel.frame.minX - 4;
    width = left - self.title.frame.minX;
    self.title.preferredMaxLayoutWidth = round(width)
    container.layoutIfNeeded()
}
john07
  • 562
  • 6
  • 16