Before ios7.0, use SizeWithFont:constrainedToSize:lineBreakMode: To calculate the height or width of the text:
The following example:
<span style= "FONT-SIZE:12PX;" >cgsize textSize = [Calltext sizewithfont:[uifont systemfontofsize:12.0] Constrainedtosize:cgsizemake (CGFLOAT _max) linebreakmode:nslinebreakbywordwrapping]; CGFloat textHeight = textsize.height;</span>
can be used to calculate the height of text in the case of a width of 200 pixels
After the ios7.0, SizeWithFont:constrainedToSize:lineBreakMode: Abandoned, replaced by a more powerful boundingRectWithSize:options:attributes: Context
The following example has the same effect as the example above:
<span style= "FONT-SIZE:12PX;" >nsdictionary *attribute = @{nsfontattributename:[uifont systemfontofsize:12.0]}; Cgsize textSize = [Calltext boundingrectwithsize:cgsizemake (0) options:nsstringdrawingtruncateslastvisibleline | Nsstringdrawinguseslinefragmentorigin Attributes:attribute context:nil].size; CGFloat textHeight = textsize.height;</span>
For more advanced use, you can read the SDK documentation in detail.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS calculates dynamic text size