2013-11-26 58 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