ios sizeWithFont:constrainedToSize:
2013-02-01 18:30
351 查看
sizeWithFont:constrainedToSize:
Returns the size of the string if it were rendered and constrained to the specified size.- (CGSize)sizeWithFont:(UIFont
*)font constrainedToSize:(CGSize)size
font
The font to use for computing the string size.
size
The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur.
CGSize sizeForFit=CGSizeMake(400, 1000);
CGSize sizeName = [
@"新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好新年好"
sizeWithFont:fontName
constrainedToSize:sizeForFit
lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"%f,%f",sizeName.width,sizeName.height);
打印结果
2013-02-01 18:08:56.541 ShopDetailInformation[5432:c07] 400.000000,60.000000
如果说sizeForFit=CGSizeMake(4000, 1000);
打印结果
2013-02-01 18:25:39.565 ShopDetailInformation[5485:c07] 1008.000000,20.000000
比较一下,我们会发现,它实际是在指定的区域sizeForFit里,来计算字符串的size大小,返回的sizeName为字符串实际所占的大小。
这个sizeForFit有点讲究,你不能设置的太大,要根据实际情况而定
相关文章推荐
- sizeWithFont:constrainedToSize:' is deprecated: first deprecated in iOS 7.0 - Use -boundingRectWithS
- IOS7.0 sizeWithFont:constrainedToSize:lineBreakMode:方法
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS7.0 sizeWithFont:constrainedToSize:lineBreakMode:方法
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS7.0 sizeWithFont:constrainedToSize:lineBreakMode:方法 iOS7 Deprecated
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS7.0 sizeWithFont:constrainedToSize:lineBreakMode:方法 iOS7 Deprecated
- [ios]tableView内部cell因内容大小变换 而变化 sizeWithFont:constrainedToSize: lineBreakMode:
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- boundingRectWithSize:options:attributes:context:代替sizeWithFont:constrainedToSize:lineBreakMode:
- sizeWithFont:constrainedToSize:转boundingRectWithSize:options:
- iOS8 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size 的替代方法
- UITextView使用 sizeWithFont:constrainedToSize:lineBreakMode:计算自适应文本高度
- sizeWithFont:constrainedToSize:lineBreakMode的使用