Wizard
Wizard

Reputation: 409

How to get the text size of a UILabel if the number of line is fixed?

I have to determine that if a UILabel has text that fits the size. I can use boudingRectWithSize:options:attributes:context but I cannot explicitly set the number of line. In the deprecated method, I can only set line break mode. Does any one know how to get the size of some text in a label if the number of line is determined (say label.numberofline = 2)?

Upvotes: 0

Views: 109

Answers (1)

michaelsnowden
michaelsnowden

Reputation: 6192

Easy

CGRect frame = [text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
                   options:NSStringDrawingUsesLineFragmentOrigin
                attributes:@{NSFontAttributeName : label.font}
                   context:nil];
CGSize size = frame.size;
NSLog(@"Size of frame %@", NSStringFromCGSize(size));

Upvotes: 1

Related Questions