Search code examples
iphoneuitableviewuilabelautoresize

How make UILabel with autoresizingMask in a custom cell?


I have a custom uitableviewcell with several labels and I would like for some of them to autoresize their frame (width) based on the content (text). I am not sure how to accomplish that. I tried to set fixed frame of the label and after apply autoresizingMask, but that doesn't do the trick. Any *pointer to a sample?


Solution

  • Use the following method:

    CGSize *size = [label.text sizeWithFont:fontOfLabelText];
    float widthOfLabel = size.width;
    

    size.width will return the actual width that the text in the label will occupy on the screen. Set the label width equal to width of text.