Adjust UILabel height depending on the text

前端 未结 30 1770
走了就别回头了
走了就别回头了 2020-11-22 03:53

Consider I have the following text in a UILabel (a long line of dynamic text):

Since the alien army vastly outnumbers the team, players m

相关标签:
30条回答
  • 2020-11-22 04:10

    Thanks guys for help, here is the code I tried which is working for me

       UILabel *instructions = [[UILabel alloc]initWithFrame:CGRectMake(10, 225, 300, 180)];
       NSString *text = @"First take clear picture and then try to zoom in to fit the ";
       instructions.text = text;
       instructions.textAlignment = UITextAlignmentCenter;
       instructions.lineBreakMode = NSLineBreakByWordWrapping;
       [instructions setTextColor:[UIColor grayColor]];
    
       CGSize expectedLabelSize = [text sizeWithFont:instructions.font 
                                    constrainedToSize:instructions.frame.size
                                        lineBreakMode:UILineBreakModeWordWrap];
    
        CGRect newFrame = instructions.frame;
        newFrame.size.height = expectedLabelSize.height;
        instructions.frame = newFrame;
        instructions.numberOfLines = 0;
        [instructions sizeToFit];
        [self addSubview:instructions];
    
    0 讨论(0)
  • 2020-11-22 04:13

    The easiest and better way that worked for me was to apply height constraint to label and set the priority to low, i.e., (250) in storyboard.

    So you need not worry about calculating the height and width programmatically, thanks to storyboard.

    0 讨论(0)
  • 2020-11-22 04:13

    Updated Method

    + (CGFloat)heightForText:(NSString*)text font:(UIFont*)font withinWidth:(CGFloat)width {
    
        CGSize constraint = CGSizeMake(width, 20000.0f);
        CGSize size;
    
        CGSize boundingBox = [text boundingRectWithSize:constraint
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:@{NSFontAttributeName:font}
                                                      context:nil].size;
    
        size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
    
        return size.height;
    }
    
    0 讨论(0)
  • 2020-11-22 04:13
    UILabel *itemTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10,100, 200.0f)];
    itemTitle.text = @"aseruy56uiytitfesh";
    itemTitle.adjustsFontSizeToFitWidth = NO;
    itemTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    itemTitle.font = [UIFont boldSystemFontOfSize:18.0];
    itemTitle.textColor = [UIColor blackColor];
    itemTitle.shadowColor = [UIColor whiteColor];
    itemTitle.shadowOffset = CGSizeMake(0, 1);
    itemTitle.backgroundColor = [UIColor blueColor];
    itemTitle.lineBreakMode = UILineBreakModeWordWrap;
    itemTitle.numberOfLines = 0;
    [itemTitle sizeToFit];
    [self.view addSubview:itemTitle];
    

    use this here all the properties are used on the label and test it by increasing the text in the itemTitle.text as

    itemTitle.text = @"diofgorigjveghnhkvjteinughntivugenvitugnvkejrfgnvkhv";
    

    it will show the perfetc answer as you need

    0 讨论(0)
  • This method will give perfect height

    -(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{
    CGSize constraint = CGSizeMake(width , 20000.0f);
    CGSize title_size;
    float totalHeight;
    
    
    title_size = [text boundingRectWithSize:constraint
                                    options:NSStringDrawingUsesLineFragmentOrigin
                                 attributes:@{ NSFontAttributeName : font }
                                    context:nil].size;
    
    totalHeight = ceil(title_size.height);
    
    CGFloat height = MAX(totalHeight, 40.0f);
    return height;
    }
    
    0 讨论(0)
  • 2020-11-22 04:14

    To do this in Swift3 following is the code:

     let labelSizeWithFixedWith = CGSize(width: 300, height: CGFloat.greatestFiniteMagnitude)
                let exactLabelsize = self.label.sizeThatFits(labelSizeWithFixedWith)
                self.label.frame = CGRect(origin: CGPoint(x: 20, y: 20), size: exactLabelsize)
    
    0 讨论(0)
提交回复
热议问题