kr15hna
kr15hna

Reputation: 539

How to get height of label inside UITableViewCell?

Say I have a simple tableViewCell with only one view(UILabel) and tableView has 10 rows and text for label at different positions is varying from 1 to 3 lines , now in cellForRowAtIndexPath

cellForRowAtIndexPath{
  [MyCell* cell = _tableView.dequeueReusableCellWithIdentifier:@"MyCell"];
  cell.label.text = [stringArray objectAtIndex:indexPath.row];
}

now i want to add cornerRadius to that label which is proportional to height of that label (say corner radius is half of label height)

label.layer.cornerRadius = label.frame.size.height/2;

now my question is where do i get to set this corner radius for label ??

  1. I tried to set in cellForRowAtIndexPath but i was getting wrong height value
  2. I tried to override layoutForSubViews in MyCell class like this.

    -(void)layoutSubviews{
      [super layoutSubviews];
       self.label.layer.cornerRadius = self.label.frame.size.height/2;
    }
    

    but still i was getting wrong height value

  3. I tried to do this at -willDisplayCell callback from tableView and still wrong height

only thing that seems to work is to override drawRect of the cell and assign cornerRadius. Is this the only way to do this ?

Upvotes: 0

Views: 985

Answers (4)

Lal Krishna
Lal Krishna

Reputation: 16160

To calculate size sizeWithFont constrainedToSize:lineBreakMode: use this method.

Example:

// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//update label with new Height
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

And set CornerRadius with newFrame value

Upvotes: 0

Raza.najam
Raza.najam

Reputation: 769

Use below code to get Label height.

- (CGFloat)getLabelHeight:(UILabel*)label
{
    CGSize constraint = CGSizeMake(label.frame.size.width, 999);
    CGSize size;
    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;
    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
    return size.height;
}

Upvotes: 0

Neha Gupta
Neha Gupta

Reputation: 539

You can get the right height of UITableViewCell label inside sizeForItemAtIndexPath.

Use below code

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSMutableDictionary *currentObject = [yourMutableArray objectAtIndex:indexPath.row];

    NSString *datum = [currentObject valueForKey:@"yourKeyName"];

    CGSize size = [datum boundingRectWithSize:CGSizeMake(self.view.bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:@"yourFontFamily" size:16]} context:nil].size;

    return size;
}

Upvotes: 0

void
void

Reputation: 1365

You also can try using [cell.label sizeToFit]; and access frame after this, in cellForRowAtIndex path for example.

But size to fit can lead to a layout bug.

Also, @DSDharma comment should be useful, but you need to override - (void) layoutSubviews right in UITableViewCell subclass. That should work.

Upvotes: 1

Related Questions