您的位置:首页 > 移动开发 > IOS开发

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有点讲究,你不能设置的太大,要根据实际情况而定
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS
相关文章推荐