1

I have used

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

and return newHeight;

to compute dynamic height of table cell. But in iOS 7 this is no more. So does iOS 7 has similar like this to compute dynamic height of cell or pickerview row height.

Updated Code:

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;

I want to set font size of text i am setting for picker view in following method.

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title=@"The Grand Bhagvati S G Highway";
return title;
 }
rmaddy
  • 307,833
  • 40
  • 508
  • 550
vivek bhoraniya
  • 1,516
  • 2
  • 17
  • 35
  • 1
    possible duplicate of [deprecated in iOS 7 " sizeWithFont: constrainedToSize: lineBreakMode: " how can I replacement?](http://stackoverflow.com/questions/18903304/deprecated-in-ios-7-sizewithfont-constrainedtosize-linebreakmode-how-can) – Amar Nov 26 '13 at 09:38

3 Answers3

2

For IOS 7 calculate Size of text,You could try this:

      CGSize size = CGSizeMake(set_your_size);

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

     CGSize textSize = textRect.size;

EDIT:

For more detail check this:

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

Dhaval Bhadania
  • 3,091
  • 1
  • 19
  • 34
1

You can use below:

CGRect textRect = [text boundingRectWithSize:constraintSize
                                     options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:cellFont}
                                     context:nil];
Tarek Hallak
  • 18,392
  • 7
  • 58
  • 68
0

Why not use SizeToFit? create a label with a frame of the biggest frame you can give to a label and then use sizeToFit

Eli Braginskiy
  • 2,795
  • 5
  • 28
  • 46