0

I have a view and table view design using Autolayout and Constraints. I have successfully design view using Autolayout.

But I have one more requirement in table view cell content. ** I want to increase LABEL height accordingly text (Depend on content of label text- Label 1). Please review attached screen shot. I have set CELL into the table view using Storyboard. Table view cell into I have taken 3 Labels.**

Label -2 : I have set constraint label-2, Leading edge constraint-5, Trailing edge constraint-5, Top edge constraint-10, Bottom constraint-0. Here I set height of Label-2 20. It fixed and I do not want to increase them.

Label -3 : I have set constraint label-3, Leading edge constraint-5, Trailing edge constraint-5, Top edge constraint-0, Bottom constraint-5. Here I set height of Label-3 20. It fixed and I do not want to increase them.

I want to increase height of Label 1 according to text.

**_Label -1 :_** I want to increase size of Label 1. I have set constraint label-1, Leading edge constraint-5, Trailing edge constraint-5, Top edge constraint-5, Bottom constraint-10. Here I set height of Label-1 35.

Right now, I have not set constraint of Label-1 height constraint. I have also tried set height constraint but it’s cannot take effect on height increases issues. I have created method for Label-1. It was count label-1 size and increases it accordingly. But it was not return current height.

Here, I set table view cell size 95. And it will be increase dynamically using table view height delegate method.

_
Source Code :-_

UITableViewSource Method:

//Count Height of table view cell

       public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath){

        TableViewCellClass cell = null;
        cell = (TableViewCellClass)tableView.DequeueReusableCell(cellIdentifier);

        return cell.RequiredTableViewCellHeight(tableView.Bounds.Size.Width);
    }


   public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath){

        TableViewCellClass cell = (TableViewCellClass)tableView.DequeueReusableCell(cellIdentifier,indexPath);

        //Add Data In Cell
        cell.Updatecell (tableItems [indexPath.Row].Label1Data, tableItems [indexPath.Row].Label2Data, tableItems [indexPath.Row].Label3Data);

        return cell;

    }

TableView Cell Method:-

 partial class TableViewCellClass : UITableViewCell{

    public TableViewCellClass (IntPtr handle) : base (handle)
    {

    }

    public void Updatecell (string lbl1Data, string lbl2Data, DateTime lbl3Data){

        Label1.Text = lbl1Data; //I want to increases height of label1

        Label2.Text = lbl2Data;
        Label3.Text = lbl3Data.ToString();

        //Here Below, fixed text and size for Label-2 and Label-3. It was fixed and never increase it's height
        Label2.SizeToFit();
        Label2.AdjustsFontSizeToFitWidth = true;

        Label3.SizeToFit();
        Label3.AdjustsFontSizeToFitWidth = true;
    }

//RequiredTableViewCellHeight Method For Count Label Hight and Increase Cell According to it.

   public float RequiredTableViewCellHeight(nfloat screenWidth){

        Console.WriteLine("screenWidth : {0}",screenWidth); //Here we get the screen size of table view WIDTh

        //Here make dynamic lable
        Label1.Lines = 0;
        Label1.AdjustsFontSizeToFitWidth = false;

        UIFont cellFont = Label1.Font;

        nfloat lbl1widthVal = screenWidth - 10; //Here we Minus 10 due to leading and trailing constraint. So Width not effected.

        //Below Count the size of label height accordingly text and font size. Size not return actuall size. It's some time return always same.
        CGSize lbl1Size = ((NSString) Label1.Text).StringSize(cellFont,constrainedToSize:new CGSize(lbl1widthVal,400.0f),lineBreakMode:UILineBreakMode.WordWrap);

        Console.WriteLine("lbl1Size Total Size: {0}",lbl1Size);

        int rowHeight = (int)lbl1Size.Height; //Get the Height of table cell
        Console.WriteLine("rowHeight: {0}",rowHeight);

        rowHeight += 60; //Add extra 60 due to label-2, label-3 size and spacing value in cell
        Console.WriteLine("After Final Edit rowHeight value: {0}",rowHeight);

        return (float)rowHeight;
    }


}

Can i fetch issues due to autolayout or I do make mistake somewhere in code?
Please note that, I do not want to remove autolayout in project. Please review that and let us know your suggestions. Thank in advance!

iOS Dev
  • 115
  • 7
  • Check out the answer in this link http://stackoverflow.com/questions/16009405/uilabel-sizetofit-doesnt-work-with-autolayout-ios6 – jogshardik Sep 26 '15 at 09:13

1 Answers1

0

Self sizing views (tableviewcells) might be of use to you.

You set the constraints, then set tableView.estimatedRowHeight = 44.0 (for example) Then set tableView.rowHeight = UITableViewAutomaticDimension

Here is a tutorial about this: http://www.appcoda.com/self-sizing-cells/

Vlk
  • 99
  • 5