Change the height of UILabel dynamically based on content

北战南征 提交于 2019-12-17 16:31:48

问题


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.


回答1:


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



回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/25157209/change-the-height-of-uilabel-dynamically-based-on-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!