Adjust UILabel height depending on the text

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

    Thanks for this post. It helped me a great deal. In my case I am also editing the text in a separate view controller. I noticed that when I use:

    [cell.contentView addSubview:cellLabel];
    

    in the tableView:cellForRowAtIndexPath: method that the label view was continually rendered over the top of the previous view each time I edited the cell. The text became pixelated, and when something was deleted or changed, the previous version was visible under the new version. Here's how I solved the problem:

    if ([[cell.contentView subviews] count] > 0) {
        UIView *test = [[cell.contentView subviews] objectAtIndex:0];
        [test removeFromSuperview];
    }
    [cell.contentView insertSubview:cellLabel atIndex:0];
    

    No more weird layering. If there is a better way to handle this, Please let me know.

提交回复
热议问题