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?
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?
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.
set the new frame to the label now.
Hope it works.It is all about properly setting autolayout.Try it and let me know.