Reputation: 31302
I am using setNumberOfLines
and sizeToFit
to resize my label in tableView cells dynamically. When there are more than 2 lines, my label will stretch to upside, but I want to make it stretch down.
let's say I have label1
and label2
. their y
position is the same before setting label.text
:
label1 label2
--------- ---------
| | | |
--------- ---------
label1 has only one line while label2 has two lines after setting label.text
separately. it looks like this now:
label1 label2
---------
--------- | line1 |
| line1 | | line2 |
--------- ---------
however, what I want is:
label1 label2
--------- ---------
| line1 | | line1 |
--------- | line2 |
---------
I believe there is a simple way to achieve my goal, but I can't find it by googling with the keyword I can think up.
my code:
myLabel.text = value;
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
Upvotes: 0
Views: 944
Reputation: 31302
To prevent a label in a table view cell from stretching up, you have to make a Top Space to
constraint for the label on storyboard:
you can make it by dragging your label to near the top of the cell, and Xcode would generate it automatically. you can modify the value of the constraint by clicking the button on the right-bottom of this picture.
After making the constraint, the label won't stretch up anymore.
Upvotes: 1
Reputation: 10175
Every answer posted before can be used for your problem with some additions. It's important that your text is into a UITableViewCell (custom or not). If your value returned by tableView:heightForRowAtIndexPath:
is greater than the label height then the code will work, if not, the label will be wrongly displayed.
So you have to return the correct value from tableView:heightForRowAtIndexPath:
in order to display the content as you need, so please compute the needed height of the text that you want to display in the tableView:heightForRowAtIndexPath:
(+- other values if you have other views).
Also if you are using autloayout check the constraints on your cell views, if not check the spring and struts on your cell views.
Upvotes: 0
Reputation: 1730
Try this:
NSString *text1 = @"Your text for label2";
CGSize constraint1 = CGSizeMake(125, 2000); //width of your label....
CGSize size1 = [text1 sizeWithFont:[UIFont fontWithName:@"ArialMT" size:12.0] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap]; //font of your label
cell.myLabel.frame = CGRectMake(posx,posy,125,size1.height);
//posy is same as for label1
cell.myLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.myLabel.numberOfLines = 0;
cell.myLabel.text = text1;
Upvotes: 1
Reputation: 13222
sizeToFit
will move the text content to the top of the frame. Refer diagram in this answer.
Instead make use of sizeWithFont:constrainedToSize:lineBreakMode:
API to get exact height of the text and then set this to your label.
Upvotes: 0
Reputation: 20021
To achieve this you have to specifically set the frame according to the text so that the label to show so
CGSize maximumLabelSize = CGSizeMake(Width,VeryLargeHeight);
CGSize expectedLabelSize = [string sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:label.lineBreakMode];
expectedLabelSize.height is the height of label to set in frame and set numberofLines to 0
Upvotes: 0