网上看了很多例子,都是用这个方法实现
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode;
不过查阅apple文档发现,在iOS7中用
boundingRectWithSize:options:attributes:context:替代了。
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
Parameters
参数:1 size label的大小 2 string 绘制选项 3 属性 4 上下文。返回一个给定的选项和特征值给这个矩形边界给接受者。这个矩形边界的origin值为第一个符号的origin值。 下面是原文:
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 of NSString 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.
Discussion
To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.
This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.