iphone:sizeWithFont方法不识别字符串中的'\n'

Bha*_*tap 5 iphone size dynamic uilabel

我使用下面的代码来计算字符串标签的大小.

    NSString * str = @"It's \n that \n don't \n";
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Verdana" size:12.5] forWidth:300  
     lineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"Height : %f   Width : %f ",size.height,size.width);
Run Code Online (Sandbox Code Playgroud)

但是id不考虑\n字符串中的字符来改变行.当我在标签上显示此字符串并将numberOfLines属性设置为4或5时,标签会考虑\n并转到下一行.考虑到字符串中的\n,我如何计算标签的高度.请帮我 .

谢谢 !!!

rck*_*nes 5

试试这个:

NSString * str = @"It's \n that \n don't \n";
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Verdana" size:12.5] constrainedToSize:CGSizeMake(300.0f, 999999.0f)];
NSLog(@"Height : %f   Width : %f ",size.height,size.width);
Run Code Online (Sandbox Code Playgroud)

这给了我Height : 48.000000 Width : 40.000000,但一定要设置标签中的行数0.