Search code examples
iosiphoneuitableviewautolayoutuilabel

Multiple line UIlabel in UItableviewcell using Autolayout


I have set setMaXPreferredLayoutWidth to UILabel and added all the required constraints to the UIlabel now I can see multiline UIlabel in my UItableviewcell but my problem is I am getting extra padding at the top and bottom of my UIlabel based on MaxpreferredLayoutWidth value.

When I set the MaxPreferreLayoutWidth to 200 I get this If maxPreferredLayoutwidth set to 200

When I set the MaxPreferreLayoutWidth to 100 I get this

MaxpreferredLayoutWidth  = 100

In general UIlabel height depends based on preferredMaxLayoutWidth. Can any one say me how to remove this extra padding and render the exact height of UIlabel?


Solution

  • We have to set the tableviewcell's frame while calculating the height of the cell in heightForRowAtIndexPath
    Ex:

    [tableViewCell setFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(tableViewCell.bounds))];
    

    and then in layoutSubviews method of UItableviewcell we have to set the preferredMaxLayoutwidth
    Ex:

    [self.multilineLabel setPreferredMaxLayoutWidth:CGRectGetWidth(self.bounds)];