0
votes

I have a UITableViewCell, and i am using UITableViewAutomaticDimension to automatically calculate the row height.

(I am replicating design of Facebook comments from iOS)

I need all the 4 label's height adjusted to height of their content.

CC = Content compression CH = Content hugging [Horizontal/Vertical]

Label 1: CC = 750/750 CH = 249/750

Label 2: CC = 999/999 CH = 999/999

Label 3: CC = 750/749 CH = 249/749

Label 4: CC = 1000/1000 CH = 1000/1000

Here, Label 2 and Label 4 are the most important labels. I dont want them to shrink anyhow. Label 1 and Label 3 can shrink but to a limit.(let's say 30px)

My cell is as follows: enter image description here

And i get this result: enter image description here

Let me know if anyone need more info.

2

2 Answers

1
votes

I've tried to replicate your table view cell structure and it seems the issue is that preferredMaxLayoutWidth of labels is not set.

You can either set it explicitly in Interface Builder or override table view cell's updateViewConstraints method:

-(void)updateViewConstraints {
    CGFloat gap = 40.0; // sum of leading and trailing space
    [super updateViewConstraints];
    label.preferredMaxLayoutWidth = self.view.bounds.size.width - gap;
}
0
votes

You set layout constraint width and Lines = 0 for both Label2 & Label4. See image below: enter image description here

Label1 set top constraint with superview, otherwise set Vertical Spacing with above label. Below is the result with large and short content for Label3.

enter image description here

enter image description here

Edit: Your custom cell like here: enter image description here

And you should update layout depending element view container.