How to calculate UILabel width based on text length? How to calculate UILabel width based on text length? swift swift

How to calculate UILabel width based on text length?


CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font                         constrainedToSize:maximumLabelSize                         lineBreakMode:yourLabel.lineBreakMode]; 

What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?

this question might have your answer, it worked for me.


For 2014, I edited in this new version, based on the ultra-handy comment by Norbert below! This does everything. Cheers

// yourLabel is your UILabel.float widthIs =  [self.yourLabel.text  boundingRectWithSize:self.yourLabel.frame.size                                             options:NSStringDrawingUsesLineFragmentOrigin  attributes:@{ NSFontAttributeName:self.yourLabel.font }  context:nil]   .size.width;NSLog(@"the width of yourLabel is %f", widthIs);


yourLabel.intrinsicContentSize.width for Objective-C / Swift


In swift

 yourLabel.intrinsicContentSize().width