2

I have a UICollectionView as shown below.The titleLabel as you can see should vary its height as per the content.the background view labelView also should change its height.How can i do that?

collectionview label

i used...

-(CGFloat) heightForText:(NSString *)text withWidth:(CGFloat) textWidth{

    CGSize constraint = CGSizeMake(textWidth, 20000.0f);
    CGRect rect = [text boundingRectWithSize:constraint
                       options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                    attributes:@{NSFontAttributeName:cell.titleLabel.font}
                       context:nil];
    CGFloat height = rect.size.height;

    height = ceilf(height);
//    NSLog(@"height %f", height);
    return height;
}

i used this like ...

CGRect newFrame = cell.titleLabel.frame;
      newFrame.size.height = height;
       cell.titleLabel.frame = newFrame;

I am getting the new frame to the label.but the height increases from a fixed y towards down.Here now i have to lift the y according to the height.Is there any other way?

  • This thread might help you http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7 – Bhumit Mehta Jun 19 '15 at 06:42

2 Answers2

4

Here goes the method

-(CGFloat) heightForText:(NSString *)text withWidth:(CGFloat) textWidth{

    CGSize constraint = CGSizeMake(textWidth, 20000.0f);
    CGRect rect = [text boundingRectWithSize:constraint
                       options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                    attributes:@{NSFontAttributeName:cell.titleLabel.font}
                       context:nil];
    CGFloat height = rect.size.height;

    height = ceilf(height);
//    NSLog(@"height %f", height);
    return height;
}
Ankit Sachan
  • 7,690
  • 16
  • 63
  • 98
  • `[UIFont systemFontOfSize:14]` should be replaced with `cell.titleLabel.font` – Bhumit Mehta Jun 19 '15 at 06:44
  • I am getting the height.But the height of label in cell is not changing.Why is it so? –  Jun 19 '15 at 07:13
  • I am getting the new frame to the label.but the height increases from a fixed y towards down.Here now i have to lift the y according to the height.Is there any other way?Getting the height is not my requirement.I have to align the label as in the image.HOw to do this?please help. –  Jun 19 '15 at 07:50
1

Use ankit's code to get the Height required for the label and use autoLayout as shown below.

Add a view ---> set leading,trailing and bottom constraints to it with respect to the cell superView.

Then add the label as subView to this view.ADD all the four constraints.ie,set leading,trailing,top and bottom constraints

now, use the above Ankit's code (I have pasted below) to get the height of the label and set it to the label frame.

-(CGFloat) heightForText:(NSString *)text withWidth:(CGFloat) textWidth{

    CGSize constraint = CGSizeMake(textWidth, 20000.0f);
    CGRect rect = [text boundingRectWithSize:constraint
                       options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                    attributes:@{NSFontAttributeName:cell.titleLabel.font}
                       context:nil];
    CGFloat height = rect.size.height;

    height = ceilf(height);
//    NSLog(@"height %f", height);
    return height;
}

set the new frame to the label now.

CGRect newFrame = cell.titleLabel.frame;
      newFrame.size.height = height;
       cell.titleLabel.frame = newFrame;

Hope it works.It is all about properly setting autolayout.Try it and let me know.

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70