I need to find the on screen size (CGSize) of a string that will be drawn in a UILabel. I know that I can use:
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
To find the size that a string will be, however, it does not take into account the maximum number of lines that UILabel lets you specify.
To rephrase if my UILabel has numberOfLines set to 2, and it receives a long string that gets truncated, how do I find the size (CGSize) of the string that is going to be drawn by the UILabel?
Thanks!
You can get the font’s
lineHeightproperty and multiply that by the number of lines, then feed that in as the maximum height of theconstrainedToSize:parameter. Something like the following: