UILabel sizeThatFits not working
20,875
Solution 1
+ (CGFloat)heightWithText:(NSString *)text
{
SizingLabel.text = text;
CGSize labelSize = [SizingLabel sizeThatFits:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)];
return (TOP_MARGIN + labelSize.height + BOTTOM_MARGIN);
}
Solution 2
As said above, sizeThatFits:
(and thus sizeToFit
) do not work well with UILabel
objects.
You better use the preferred textRectForBounds:limitedToNumberOfLines:
method:
+ (CGFloat)heightWithText:(NSString *)text
{
resizingLabel.text = text;
CGSize labelSize = [resizingLabel textRectForBounds:CGRectMake(0.0, 0.0, LABEL_WIDTH, CGFLOAT_MAX)
limitedToNumberOfLines:0].size; // No limit
return (TOP_MARGIN + labelSize.height + BOTTOM_MARGIN);
}
Solution 3
Do this in your Custom Cell Class:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//Message Label
lbl_name = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 25)];
[lbl_name setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
lbl_name.lineBreakMode = UILineBreakModeWordWrap;
lbl_name.numberOfLines = 0;
[lbl_name sizeToFit];
[self.contentView addSubview:lbl_name];
//Time
}
return self;
}
-(void)resizeNameLabel:(NSString *)text
{
CGSize constraint = CGSizeMake(300 , 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[lbl_name setFrame:CGRectMake(10, 5, 300, MAX(size.height, 25.0f))];//300 Label Width
[lbl_name setText:text];
}
Do this in main class..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = (CustomCell *)[[CustomCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
[((CustomCell *)cell) resizeNameLabel:text];
return cell;
}
Just do like this...
Author by
Sean Danzeiser
Updated on March 29, 2020Comments
-
Sean Danzeiser about 4 years
I'm trying to calculate the height for a UITableViewCell so I've defined a class method that looks like this
+ (CGFloat)heightWithText:(NSString *)text { SizingLabel.text = text; [SizingLabel sizeThatFits:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)]; return (TOP_MARGIN + SizingLabel.frame.size.height + BOTTOM_MARGIN); }
I've defined SizingLabel like this:
+ (void)initialize { SizingLabel = [[UILabel alloc] initWithFrame:CGRectZero]; SizingLabel.numberOfLines = 0; SizingLabel.lineBreakMode = NSLineBreakByWordWrapping; }
However, if i stick a breakpoint in the -heightWithText: method, i notice that the dimensions of SizingLabel never change and i thus get an incorrect value. Why is that?
-
Jing over 10 yearsthe (NSDictionary) attributes is where you set your text attributes
-
Daniel over 9 yearsDoes not work for me, also, you should follow the naming convention and start variable names with lower case (or is
SizingLabel
supposed to be a class?) -
Rivera over 9 years
SizingLabel
must have been an automatic rename problem. Also you or anyone can edit posts ;) As for this not working. What part is the problem? Basically this code only uses the built-intextRectForBounds:limitedToNumberOfLines:
which should always work. -
Daniel over 9 yearsit was returning a size that did not really reflect the size the text would be given. After all, it does not take into account the font. I ended up using
NSString boundingRectWithSize:options:attributes:context:
. -
Rivera over 9 yearsIt does take into account the
UILabel
font and parameters. You can set it up all in IB and then have an accurate measure. That is why I prefer it than trying to recreate the label configuration withNSString
. As for your problem, is the built-in method returning a wrong value or is it the accompanying adjustments? And theSizingLabel
came from the original question. -
Daniel over 9 yearsIf you believe this comment, it seems that it does not work correctly when the attributed string does not contain a
NSFontAttributeName
. -
OlgaVogue over 8 yearsWorks for UILabel if font is set for the label.
-
Kevin over 8 years@OlgaVogue No, it only works sometimes. sizeThatFits is notoriously buggy.
-
Darren Ehlers almost 6 yearsI've used sizeThatFits: for years, and it seems to be a bit of voodoo, sometimes works and sometimes just doesn't. Using textRectForBounds is perfect!