I am trying to set the height of my label inside of my UITableViewCell dynamically at runtime. The height of the label is dependent on the content. I have set in my custom UITableViewCell xib file that the UILabel should have 0 lines (no bounds). Below is my code for CellForRowAtIndexPath. I've looked around and tried a few things but nothing seems to work for me.
XYZPlayerTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = indexPath.row;
static NSString *CellIdentifier = @"PlayerCell";
XYZPlayerTableViewCell *cell = (XYZPlayerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
XYZPlayer *player = [self.players objectAtIndex:row];
NSString *nameText = player.name;
NSString *gameText = player.game;
cell.nameLabel.text = nameText;
cell.gameLabel.text = gameText;
[cell.gameLabel sizeToFit];
cell.gameLabel.numberOfLines = 0;
cell.ratingImageView.image = [self imageForRating:player.rating];
CGSize boundingRect = CGSizeMake(cell.frame.size.width, 4000);
CGRect expectedFrame = [cell.gameLabel.text boundingRectWithSize:boundingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: cell.gameLabel.font} context:nil];
cell.gameLabel.frame = expectedFrame;
NSLog(@"gameLabel = 0x%@ gameText = %@ gameLabelHeight = %.2f", cell.gameLabel, gameText, expectedFrame.size.height);
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = indexPath.row;
//set width depending on device orientation
self.cellPrototype.frame = CGRectMake(self.cellPrototype.frame.origin.x, self.cellPrototype.frame.origin.y, tableView.frame.size.width, self.cellPrototype.frame.size.height);
// Get variables
XYZPlayer *player = [self.players objectAtIndex:row];
UILabel *nameLabel = self.cellPrototype.nameLabel;
NSString *nameText = player.name;
UILabel *gameLabel = self.cellPrototype.gameLabel;
NSString *gameText = player.game;
// Calculate height
CGFloat nameLabelHeight = [self sizeOfLabel:nameLabel withText:nameText].height;
CGFloat gameLabelHeight = [self sizeOfLabel:gameLabel withText:gameText].height;
CGFloat padding = nameLabel.frame.origin.y;
CGFloat combinedHeight = padding + nameLabelHeight + padding/2 + gameLabelHeight + padding;
return combinedHeight;
}
XYZPlayerTableViewCell.m
-(void)setGameLabelHeightForString:(NSString *)string
{
self.gameLabel.numberOfLines = 0;
gameLabel.text = string;
CGSize maximumLabelSize = CGSizeMake(gameLabel.frame.size.width, FLT_MAX);
CGSize expectedLabelSize = [string sizeWithFont:self.gameLabel.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = self.gameLabel.frame;
newFrame.size.height = expectedLabelSize.height;
self.gameLabel.frame = newFrame;
}
-(void) layoutSubviews
{
[self setGameLabelHeightForString:gameLabel.text];
}
The output at runtime is this:
2014-07-09 00:21:32.277 MyTestApp[24503:60b]
gameLabel = 0x< UILabel: 0x8e42800; frame = (0 0; 321.946 81.124); text = 'Tic-Tac-Toe And another l...'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = < CALayer: 0x8e360d0>>
gameText = Tic-Tac-Toe And another line and maybe some more because who know's how long this can go. I think i need just a little bit more text to make this super obvious
gameLabelHeight = 81.12
And this is what my app looks like. The gray color shows the height of my label
Any one got any ideas of what I'm doing wrong?
– tableView:heightForRowAtIndexPath:
method? or setrowHeight
property of UITableView . – Toseef KhiljinumberOfLines
ofgameLabel
to zero. – Shiva Kumar Ganthi