1

I can't get this dynamic label to resize its height according to the content. It logs the correct info but the label is always the same height. Any ideas? (code based on this answer)

        self.descriptionLabel.text = string;
        self.descriptionLabel.adjustsFontSizeToFitWidth = YES;
      
        
        self.descriptionLabel.numberOfLines=0;
        CGSize maximumLabelSize = CGSizeMake(296,9999);
        
        CGSize expectedLabelSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
        
        //adjust the label the the new height.
        CGRect newFrame = self.descriptionLabel.frame;
        newFrame.size.height = expectedLabelSize.height;
        NSLog(@"the height %f", newFrame.size.height);
        self.descriptionLabel.frame = newFrame;
Ryan M
  • 18,333
  • 31
  • 67
  • 74
vinylDeveloper
  • 687
  • 2
  • 7
  • 25

1 Answers1

0
        CGSize constraintSize = CGSizeMake(requiredWidth, MAXFLOAT);

        CGSize labelSize = [requiredLableText sizeWithFont:requiredFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

        returned size will give u the dynamic height of the UILabel
Naresh Gunda
  • 332
  • 1
  • 2
  • 12