2013-11-26 60 views
1

我已经使用动态小区大小

CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; 

并返回newHeight;

来计算表格单元的动态高度。 但在iOS 7中,这不再是。那么iOS 7也有类似的计算单元格或pickerview行高的动态高度。

更新的代码:

NSString * text = str.text;

   CGSize constraintSize = CGSizeMake(320.0f, 40.0f); // Make changes in width as per your label requirement. 

       CGRect textRect = [text boundingRectWithSize:constraintSize 
            options:NSStringDrawingUsesLineFragmentOrigin 
            attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Avenir" size:17]} 
            context:nil]; 
       CGSize size = textRect.size; 
       NSLog(@"textSize :: %f",size.height); 


       return size.height; 

我想设置文本的字体大小,我在下面的方法设置选择器视图。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
NSString *title; 
[email protected]"The Grand Bhagvati S G Highway"; 
return title; 
} 
+1

可能的重复[在iOS 7中弃用“sizeWithFont:constrainedToSize:lineBreakMode:”我怎么可以替换?](http:// stackoverflow.com/questions/18903304/deprecated-in-ios-7-sizewithfont-constrainedtosize-linebreakmode-how-can) – Amar

回答

2

对于文本的IOS 7计算尺寸,你可以试试这个:

 CGSize size = CGSizeMake(set_your_size); 

    CGRect textRect = [text boundingRectWithSize:size 
          options:NSStringDrawingUsesLineFragmentOrigin 
          attributes:@{NSFontAttributeName:[UIFont fontNamesForFamilyName:@"Helvetica"} 
          context:nil]; 

    CGSize textSize = textRect.size; 

编辑:

更多细节检查:

https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/uid/TP40011688-CH1-SW7

ios-6-ios-7-transition

+0

和boundingRectWithSize中的大小应该是多少? – vivek

+0

@vivek检查我的编辑答案。 –

+0

dhaval和@null我在我的日志中得到textSize 0.0000。 – vivek

1

您可以在下面使用:

CGRect textRect = [text boundingRectWithSize:constraintSize 
            options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin 
            attributes:@{NSFontAttributeName:cellFont} 
            context:nil]; 
0

为什么不使用SizeToFit?创建一个最大框架的标签,你可以给一个标签,然后使用sizeToFit