[Str boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:options attributes:attributes context:context]
返回一个x,y=0的CGRect
Size
CGSizeMake(MAXFLOAT,MAXFLOAT)
会给出一个最合适的单行的尺寸
CGSizeMake(300,MAXFLOAT)
限定文本宽度是300
options
NSStringDrawingUsesLineFragmentOrigin 只有这个才能计算出换行的Frame,即如果要计算多行的准确高度。
attributes
描述字体的相关属性的字典,属性在UIKit的第一个头文件中
NSDictionary *textDict = @{NSFontAttributeName: textFont}
context
用得较少
写成NSString的类别方法
- (CGRect)textRectWithSize:(CGSize)size attributes:(NSDictionary *)attributes{ return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil] }
网友评论