5

I am using the below code to calculate the height of a label from string length. Im using xcode 5.0 and it works fine in iOS 6 simulator but it's not working well in iOS 7.

NSString* str = [[array objectAtIndex:i]valueForKey:@"comment"];

CGSize size = [className sizeWithFont:[UIFont systemFontOfSize:15]   
 constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

Height_1 = size.height;

If there is any solution for iOS 7 then please help. Thanks in Advance

Sr.Richie
  • 5,680
  • 5
  • 38
  • 62
mahavir
  • 77
  • 1
  • 1
  • 4
  • 3
    I'm out of close votes, but here's the dupe: http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7 This might be relevant too: http://stackoverflow.com/questions/18903304/deprecated-in-ios-7-sizewithfont-constrainedtosize-linebreakmode-how-can – Mick MacCallum Feb 25 '14 at 12:23

4 Answers4

21

Well here is a solution I use for calculating the height for iOS 6 and iOS 7, and I have passed few arguments to make it reusable.

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

/**
*  This method is used to calculate height of text given which fits in specific width having    font provided
*
*  @param text       Text to calculate height of
*  @param widthValue Width of container
*  @param font       Font size of text
*
*  @return Height required to fit given text in container
*/

+ (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font
{
    CGFloat result = font.pointSize + 4;
    if (text)
    {
        CGSize textSize = { widthValue, CGFLOAT_MAX };       //Width and height of text area
        CGSize size;
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        {
            //iOS 7
            CGRect frame = [text boundingRectWithSize:textSize
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{ NSFontAttributeName:font }
                                          context:nil];
            size = CGSizeMake(frame.size.width, frame.size.height+1);
        }
        else
        {
            //iOS 6.0
            size = [text sizeWithFont:font constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
        }
        result = MAX(size.height, result); //At least one row
    }
    return result;
}

Hope this helps and yes any suggestions are appreciated. Happy Coding :)

For iOS 7 and above use below method.

+ (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
    CGSize size = CGSizeZero;
    if (text) {
        //iOS 7
        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
        size = CGSizeMake(frame.size.width, frame.size.height + 1);
    }
    return size;
}
Pratik Mistry
  • 2,905
  • 1
  • 22
  • 35
2

Try using this

#define FONT_SIZE 15.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 20.0f


NSString *text;
CGSize constraint;
CGSize size;
CGFloat height;

text = [[array objectAtIndex:i]valueForKey:@"comment"];
constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGRect textRect = [text boundingRectWithSize:constraint
                                    options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]}
                                    context:nil];

size = textRect.size;


height = size.height;
Maverick
  • 319
  • 2
  • 13
  • 1
    +1 for `boundingRectWithSize:options:attributes:context:`, -1 for defines rather than of const static vars. – zaph Feb 25 '14 at 12:37
  • Note `boundingRectWithSize:options:attributes:context:` is only available iOS7 and above. – zaph Feb 25 '14 at 12:38
  • @Zaph Thanks for your feedback. I am new to the iOS coding. Instead of using defines I should use static vars? Will they make my code execute fast? or what they actually do. Please guide. – Maverick Feb 25 '14 at 12:41
  • http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c – Mick MacCallum Feb 25 '14 at 12:50
  • @0x7fffffff Thanks for the explaination. – Maverick Feb 25 '14 at 13:37
1
sizeWithFont

is deprecated. Use

[string sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];

instead

Avt
  • 16,927
  • 4
  • 52
  • 72
-1

Use Below Code to get the height of the Label

CGSize szMaxCell = CGSizeMake(220, 2999);
UIFont *font = [UIFont systemFontOfSize:14.0f];    // whatever font you're using to display
CGSize szCell = [yourString sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByWordWrapping];
iDeveloper
  • 498
  • 4
  • 9