2011-05-24 34 views
5

我使用下面的代碼來計算字符串標籤的大小。iphone:sizeWithFont方法不能識別字符串中的' n'

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); 

但是id不考慮字符串中的\ n個字符來改變行。當我在標籤上顯示此字符串並將其numberOfLines屬性設置爲4或5時,標籤會考慮\ n並轉到下一行。如何從字符串中計算\ n的標籤高度。請幫幫我 。

謝謝!!!

回答

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); 

這給我Height : 48.000000 Width : 40.000000,但一定要設置行的數量在標籤中0