Adjust UILabel height depending on the text

前端 未结 30 1773
走了就别回头了
走了就别回头了 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:26

    Instead doing this programmatically, you can do this in Storyboard/XIB while designing.

    • Set UIlabel's number of lines property to 0 in attribute inspector.
    • Then set width constraint/(or) leading and trailing constraint as per the requirement.
    • Then set height constraint with minimum value. Finally select the height constraint you added and in the size inspector the one next to attribute inspector, change the height constraint's relation from equal to - greater than.
    0 讨论(0)
  • 2020-11-22 04:26

    Swift 2:

        yourLabel.text = "your very long text"
        yourLabel.numberOfLines = 0
        yourLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        yourLabel.frame.size.width = 200
        yourLabel.frame.size.height = CGFloat(MAXFLOAT)
        yourLabel.sizeToFit()
    

    The interesting lines are sizeToFit() in conjunction with setting a frame.size.height to the max float, this will give room for long text, but sizeToFit() will force it to only use the necessary, but ALWAYS call it after setting the .frame.size.height .

    I recommend setting a .backgroundColor for debug purposes, this way you can see the frame being rendered for each case.

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

    This is one line of code to get the UILabel Height using Objective-c:

    labelObj.numberOfLines = 0;
    CGSize neededSize = [labelObj sizeThatFits:CGSizeMake(screenWidth, CGFLOAT_MAX)];
    

    and using .height you will get the height of label as follows:

    neededSize.height
    
    0 讨论(0)
  • 2020-11-22 04:27
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        cellIdentifier = @"myCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.myUILabel.lineBreakMode = UILineBreakModeWordWrap;        
        cell.myUILabel.numberOfLines = 0;
        cell.myUILabel.text = @"Some very very very very long text....."
        [cell.myUILabel.criterionDescriptionLabel sizeToFit];    
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        CGFloat rowHeight = cell.myUILabel.frame.size.height + 10;
    
        return rowHeight;    
    }
    
    0 讨论(0)
  • 2020-11-22 04:28

    Since sizeWithFont is deprecated I use this one instead.

    this one get label specific attributes.

    -(CGFloat)heightForLabel:(UILabel *)label withText:(NSString *)text{
    
        NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:label.font}];
        CGRect rect = [attributedText boundingRectWithSize:(CGSize){label.frame.size.width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    
        return ceil(rect.size.height);
    }
    
    0 讨论(0)
  • 2020-11-22 04:29

    sizeWithFont constrainedToSize:lineBreakMode: is the method to use. An example of how to use it is below:

    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
    
    CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   
    
    //adjust the label the the new height.
    CGRect newFrame = yourLabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    yourLabel.frame = newFrame;
    
    0 讨论(0)
提交回复
热议问题