user4990270
user4990270

Reputation:

UILabel automatic height as per the length ios

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?

Upvotes: 2

Views: 748

Answers (2)

abhimuralidharan
abhimuralidharan

Reputation: 5939

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.

Upvotes: 1

Ankit Sachan
Ankit Sachan

Reputation: 7850

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;
}

Upvotes: 4

Related Questions