3

I have a UILabel as subview of UIButton and I am passing the value from another view and populating in UILabel. Now, I want that UILabel must change its height based on the content.If text is "Hello" it must be in 1 line but if text is " my text is too long to fit in the label", it must change its size. I have used

   [self.addressLabel sizeToFit];

But for this i need to leave empty space below UILabel. Simply what I want is that when text strength increases,size of UILabel and UIView must expand.

Jerodev
  • 29,019
  • 11
  • 72
  • 94
Sushil Sharma
  • 2,231
  • 3
  • 23
  • 48
  • Possible duplicate of [Adjust UILabel height depending on the text](http://stackoverflow.com/questions/446405/adjust-uilabel-height-depending-on-the-text) – Paul Roub Apr 07 '16 at 14:27

4 Answers4

12

Using below you can get the height of the label

  • text - text of the label
  • font - font used in label
  • width - width of the label

    -(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{
        CGSize constraint = CGSizeMake(width , 20000.0f);
        CGSize title_size;
        float totalHeight;
    
        SEL selector = @selector(boundingRectWithSize:options:attributes:context:);
        if ([text respondsToSelector:selector]) {                
            title_size = [text boundingRectWithSize:constraint
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{ NSFontAttributeName : font }
                                            context:nil].size;
    
            totalHeight = ceil(title_size.height); 
        } else {                
            title_size = [text sizeWithFont:font
                          constrainedToSize:constraint
                              lineBreakMode:NSLineBreakByWordWrapping];                
            totalHeight = title_size.height ;                
        }
    
        CGFloat height = MAX(totalHeight, 40.0f);
        return height;            
    }
    

and create a frame using the height

CGRect frame = questionTitleLbl.frame;

float height = [self getHeightForText:questionTitleLbl.text 
                             withFont:questionTitleLbl.font
                            andWidth:questionTitleLbl.frame.size.width];
float gap = 2;

cell.questionTitleLbl.frame = CGRectMake(frame.origin.x, 
                                         frame.origin.y, 
                                         frame.size.width, 
                                         height);
karthik
  • 1,191
  • 1
  • 13
  • 26
3

Here is the way that i handle this issue:

UILabel *sight = (UILabel *)[cell viewWithTag:100];
sight.text=tmpGroup.title;

sight.frame =CGRectMake(sight.frame.origin.x, sight.frame.origin.y, 191, 21);


sight.font = [UIFont fontWithName:@"RobotoSlab-Bold" size:10];


sight.numberOfLines=0;
sight.lineBreakMode=NSLineBreakByWordWrapping;

[sight sizeToFit];
hoya21
  • 893
  • 7
  • 23
1

Use this code its very easy and updated with ios8

add this method to your appconstant file

inline static CGSize getLabelHeightForFont(NSString *fontName,NSString* str, float fontSize, float lblWidth)
{
    NSString *text = str;
    CGFloat width = lblWidth;
    UIFont *font = [UIFont fontWithName:fontName size:fontSize];

    NSAttributedString *attributedText =
    [[NSAttributedString alloc]
     initWithString:text
     attributes:@
     {
     NSFontAttributeName: font
     }];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    return rect.size;


}

and finally use this code for dynamic create UILabel

CGFloat lbl_height = getLabelHeightForFont(@"System- System", address, 15, lbl_address.frame.size.width);
    lbl_address.numberOfLines = 0;
    lbl_address.textAlignment = NSTextAlignmentLeft;
    [lbl_address setLineBreakMode:NSLineBreakByWordWrapping];
    lbl_address.frame = CGRectMake(lbl_address.frame.origin.x, lbl_address.frame.origin.y, lbl_address.frame.size.width, lbl_height+5);
Kishore Suthar
  • 2,683
  • 4
  • 21
  • 38
0

This is the very simplest function for getting dynamic height for labels. You can just use this function.
Here ceil is the predefind function for returns the smallest integer value. And MAXHEIGHT is maximum height for uilabel for example you can give 1000 also for future caluclations...

-(CGFloat) getHeightForLabels : (UILabel *) label
    {

        CGSize widthMaxHeight = CGSizeMake(label.frame.size.width, MAXHEIGHT);
        CGSize size;

        NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
        CGSize boundingRect = [label.text boundingRectWithSize:widthMaxHeight
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:@{NSFontAttributeName:label.font}
                                                      context:context].size;

        size = CGSizeMake(ceil(boundingRect.width), ceil(boundingRect.height));

        return size.height;
    }
Vaibhav Shiledar
  • 865
  • 8
  • 15