0

I have a tableview in which there is a uilabel inside the cell. label takes variable height on the basis of text.My codes are below

- (CGFloat)getLabelHeight:(UILabel*)label
{
  CGSize constraint = CGSizeMake(label.frame.size.width-10, 1000.0f);      
  CGSize size;

  NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:label.font}
                                              context:context].size;

  size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

  return size.height;
}

but setting constraint is difficult one. AnyOne knows the easiest way to calculate the height of cell dynamically and correct way of setting constraint.

Irfan
  • 5,070
  • 1
  • 28
  • 32
jinnie
  • 53
  • 6
  • This article is worth reading: http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift – ImWH Dec 16 '15 at 10:18
  • @jinnie if possible can you please send me sample code project I'll try to solve your problem. – Nimit Parekh Dec 16 '15 at 10:22
  • 2
    http://stackoverflow.com/questions/32652612/change-cell-height-by-the-content-of-the-textview-inside-the-cell/32653118#32653118 This is exactly what you want. – Irfan Dec 16 '15 at 10:22

1 Answers1

2

If you are using autolayout then it will be easiest.Just add top,left,right and botton constraints for your UILabel, set numberofLines to 0 and line break mode to wordWrap. and in your heightForRow method pass UITableViewAutomaticDimension.

P.S. If there is only label in your cell then instead of using custom cell , use UITableViewCell and use its text label.You then won't have to manage any constraints.

Muneeba
  • 1,756
  • 10
  • 11