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
}
Written by Tu Hoang
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Xcode
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#