IOS7 之前的方法
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
参数: font : 字体
size : 大小
return : CGSize类型 根据返回CGSize对象就可以得到 height
IOS 7 下方法:
- (CGRect)boundingRectWithSize:(CGSize)size
options:(NSStringDrawingOptions)
optionsattributes:(NSDictionary *)attributes
context:(NSStringDrawingContext *)context
Parameters
-
size
-
The size of the rectangle to draw in.
options
-
String drawing options.
attributes
-
A dictionary of text attributes to be applied to the string. These are the same attributes that can be applied to an
NSAttributedString
object, but in the case ofNSString
objects, the attributes apply to the entire string, rather than ranges within the string.
context
-
The string drawing context to use for the receiver, specifying minimum scale factor and tracking adjustments.
Return Value
The bounding rect for the receiver drawn using the given options and display characteristics. The rect origin returned from this method is the first glyph origin.
String Drawing Options
Options for customizing the way strings are drawn.
enum { NSStringDrawingTruncatesLastVisibleLine = 1 << 5, NSStringDrawingUsesLineFragmentOrigin = 1 << 0, NSStringDrawingUsesFontLeading = 1 << 1, NSStringDrawingUsesDeviceMetrics = 1 << 3, }; typedef NSInteger NSStringDrawingOptions;
-
Truncate and add an ellipsis character to the last visible line if the text does not fit into the specified bounds. This option is ignored if the
NSStringDrawingUsesLineFragmentOrigin
option is not also specified. -
The origin specified when drawing the string is the line fragment origin and not the baseline origin.
-
Use the font leading information to calculate line heights.
-
Use the image glyph bounds (instead of the typographic bounds) when computing layout.
NSStringDrawingTruncatesLastVisibleLine
NSStringDrawingUsesLineFragmentOrigin
NSStringDrawingUsesFontLeading
NSStringDrawingUsesDeviceMetrics