Adjust UILabel height depending on the text

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

    You can get height using below code

    You have to pass

    1. text 2. font 3. label width

      func heightForLabel(text: String, font: UIFont, width: CGFloat) -> CGFloat {
      
         let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
         label.numberOfLines = 0
         label.lineBreakMode = NSLineBreakMode.byWordWrapping
         label.font = font
         label.text = text
         label.sizeToFit()
      
         return label.frame.height
      }
      
    0 讨论(0)
  • myLabel.text = "your very long text"
    myLabel.numberOfLines = 0
    myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
    

    Please set constraints for UILable in storyboard including top left bottom right

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

    Here is a category version:

    UILabel+AutoSize.h #import

    @interface UILabel (AutoSize)
    
    - (void) autosizeForWidth: (int) width;
    
    @end
    

    UILabel+AutoSize.m

    #import "UILabel+AutoSize.h"
    
    @implementation UILabel (AutoSize)
    
    - (void) autosizeForWidth: (int) width {
        self.lineBreakMode = UILineBreakModeWordWrap;
        self.numberOfLines = 0;
        CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
        CGSize expectedLabelSize = [self.text sizeWithFont:self.font constrainedToSize:maximumLabelSize lineBreakMode:self.lineBreakMode];
        CGRect newFrame = self.frame;
        newFrame.size.height = expectedLabelSize.height;
        self.frame = newFrame;
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-22 04:16

    UILabel extension based on this answer for Swift 4 and above

    extension UILabel {
    
        func retrieveTextHeight () -> CGFloat {
            let attributedText = NSAttributedString(string: self.text!, attributes: [NSFontAttributeName:self.font])
    
            let rect = attributedText.boundingRect(with: CGSize(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)
    
            return ceil(rect.size.height)
        }
    
    }
    

    Can be used like:

    self.labelHeightConstraint.constant = self.label.retrieveTextHeight()
    
    0 讨论(0)
  • 2020-11-22 04:19

    You can implement TableViewController's (UITableViewCell *)tableView:cellForRowAtIndexPath method in the following way (for example) :

    #define CELL_LABEL_TAG 1
    
    - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *text = @"my long text";
    
        static NSString *MyIdentifier = @"MyIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero  reuseIdentifier:identifier] autorelease];
        }
    
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
        CGFloat height = [self textHeight:text] + 10;
        CGRect frame = CGRectMake(10.0f, 10.0f, width, height);
    
        UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
        cellLabel.tag = CELL_LABEL_TAG;
        cellLabel.textColor = [UIColor blackColor];
        cellLabel.backgroundColor = [UIColor clearColor];
        cellLabel.textAlignment = UITextAlignmentLeft;
        cellLabel.font = [UIFont systemFontOfSize:12.0f];
        [cell.contentView addSubview:cellLabel];
        [cellLabel release];
    
        return cell;
    }
    
    UILabel *label = (UILabel *)[cell viewWithTag:CELL_LABEL_TAG];
    label.text = text;
    label.numberOfLines = 0;
    [label sizeToFit];
    return cell;
    

    Also use NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to compute the text's height.

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

    Adding to the above answers:

    This can be easily achieved via storyboard.

    1. Set constraint for UILabel.(In my case I did top, left and fixed width)
    2. Set Number of line to 0 in Attribute Inspector
    3. Set Line Break to WordWrap in Attribute Inspector.

    0 讨论(0)
提交回复
热议问题