Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate UILabel height dynamically?

I want to calculate number of lines and height of UILabel dynamically from given text for same.

like image 598
Hitesh Avatar asked Aug 24 '11 10:08

Hitesh


People also ask

How do you text the height of UILabel?

text = text; label. numberOfLines = 0; [label sizeToFit]; return cell; Also use NSString 's sizeWithFont:constrainedToSize:lineBreakMode: method to compute the text's height. Show activity on this post.

How do I change the dynamic height of a label in Swift?

To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height. Let's create a label and add it as a subview to our view.


1 Answers

Try this

// UILabel *myLabel;  CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font                              constrainedToSize:myLabel.frame.size                                  lineBreakMode:NSLineBreakByWordWrapping];  CGFloat labelHeight = labelSize.height;   int lines = [myLabel.text sizeWithFont:myLabel.font                       constrainedToSize:myLabel.frame.size                           lineBreakMode:NSLineBreakByWordWrapping].height/16;               // '16' is font size 

or

int lines = labelHeight/16;  NSLog(@"lines count : %i \n\n",lines);   

or

int lines = [myLabel.text sizeWithFont:myLabel.font                       constrainedToSize:myLabel.frame.size                           lineBreakMode:UILineBreakModeWordWrap].height /myLabel.font.pointSize; //fetching font size from font 

By Using Categories, Just Create the category class named as

UILabel+UILabelDynamicHeight.h

UILabel+UILabelDynamicHeight.m

No more tension about the height calculation. Please review the below implementation.

Updates for iOS7 & Above,iOS 7 below : Dynamically calculate the UILabel height

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) #define iOS7_0 @"7.0" 

UILabel+UILabelDynamicHeight.h

#import <UIKit/UIKit.h> @interface UILabel (UILabelDynamicHeight)  #pragma mark - Calculate the size the Multi line Label /*====================================================================*/      /* Calculate the size of the Multi line Label */  /*====================================================================*/ /**  *  Returns the size of the Label  *  *  @param aLabel To be used to calculte the height  *  *  @return size of the Label  */  -(CGSize)sizeOfMultiLineLabel;  @end 

UILabel+UILabelDynamicHeight.m

#import "UILabel+UILabelDynamicHeight.h" @implementation UILabel (UILabelDynamicHeight)   #pragma mark - Calculate the size,bounds,frame of the Multi line Label /*====================================================================*/  /* Calculate the size,bounds,frame of the Multi line Label */  /*====================================================================*/ /**  *  Returns the size of the Label  *  *  @param aLabel To be used to calculte the height  *  *  @return size of the Label  */ -(CGSize)sizeOfMultiLineLabel{      //Label text     NSString *aLabelTextString = [self text];      //Label font     UIFont *aLabelFont = [self font];      //Width of the Label     CGFloat aLabelSizeWidth = self.frame.size.width;       if (SYSTEM_VERSION_LESS_THAN(iOS7_0)) {         //version < 7.0          return [aLabelTextString sizeWithFont:aLabelFont                             constrainedToSize:CGSizeMake(aLabelSizeWidth, MAXFLOAT)                                 lineBreakMode:NSLineBreakByWordWrapping];     }     else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(iOS7_0)) {         //version >= 7.0          //Return the calculated size of the Label         return [aLabelTextString boundingRectWithSize:CGSizeMake(aLabelSizeWidth, MAXFLOAT)                                               options:NSStringDrawingUsesLineFragmentOrigin                                            attributes:@{                                                         NSFontAttributeName : aLabelFont                                                         }                                               context:nil].size;      }      return [self bounds].size;  } @end 
like image 183
Vijay-Apple-Dev.blogspot.com Avatar answered Sep 19 '22 04:09

Vijay-Apple-Dev.blogspot.com