Last Updated: February 25, 2016
·
545
· rebyn

UILabel's width to fit its content

Ran into a requirement today that needs a few labels to be content-fit, here's how I did it:

- (void)resizeToContent {
    label.frame = CGRectMake(
         label.frame.origin.x,
         label.frame.origin.y,
         label.intrinsicContentSize.width + {PADDING},
         label.frame.size.height
    );
    // Padding = padding left + padding right
    // Make sure you align your label's content to center
}