Adjust UILabel height depending on the text

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

提交回复
热议问题