Dynamically getting height of UILabel according to Text return different Value for iOS 7.0 and iOS 6.1
Solution 1
You have to dynamically set the frame, like below:
Tested on iOS 6 to iOS 12.2
Swift:
let constrainedSize = CGSize(width: self.titleLable.frame.size.width, height:9999)
let attributesDictionary = [NSAttributedString.Key.font: UIFont.init(name: "HelveticaNeue", size: 11.0)]
let string = NSAttributedString.init(string: "textToShow", attributes: attributesDictionary as [NSAttributedString.Key : Any])
var requiredHeight = string.boundingRect(with: constrainedSize, options: .usesLineFragmentOrigin, context: nil)
if (requiredHeight.size.width > self.titleLable.frame.size.width) {
requiredHeight = CGRect(x: 0, y: 0, width: self.titleLable.frame.size.width, height: requiredHeight.size.height)
}
var newFrame = self.titleLable.frame
newFrame.size.height = requiredHeight.size.height
self.titleLable.frame = newFrame
Objective C:
CGSize constrainedSize = CGSizeMake(self.resizableLable.frame.size.width , 9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"HelveticaNeue" size:11.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"textToShow" attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > self.resizableLable.frame.size.width) {
requiredHeight = CGRectMake(0,0, self.resizableLable.frame.size.width, requiredHeight.size.height);
}
CGRect newFrame = self.resizableLable.frame;
newFrame.size.height = requiredHeight.size.height;
self.resizableLable.frame = newFrame;
Solution 2
Here's a total solution for width and height. Put these in your AppDelegate:
+(void)fixHeightOfThisLabel:(UILabel *)aLabel
{
aLabel.frame = CGRectMake(aLabel.frame.origin.x,
aLabel.frame.origin.y,
aLabel.frame.size.width,
[AppDelegate heightOfTextForString:aLabel.text
andFont:aLabel.font
maxSize:CGSizeMake(aLabel.frame.size.width, MAXFLOAT)]);
}
+(CGFloat)heightOfTextForString:(NSString *)aString andFont:(UIFont *)aFont maxSize:(CGSize)aSize
{
// iOS7
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
CGSize sizeOfText = [aString boundingRectWithSize: aSize
options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes: [NSDictionary dictionaryWithObject:aFont
forKey:NSFontAttributeName]
context: nil].size;
return ceilf(sizeOfText.height);
}
// iOS6
CGSize textSize = [aString sizeWithFont:aFont
constrainedToSize:aSize
lineBreakMode:NSLineBreakByWordWrapping];
return ceilf(textSize.height;
}
+(void)fixWidthOfThisLabel:(UILabel *)aLabel
{
aLabel.frame = CGRectMake(aLabel.frame.origin.x,
aLabel.frame.origin.y,
[AppDelegate widthOfTextForString:aLabel.text
andFont:aLabel.font
maxSize:CGSizeMake(MAXFLOAT, aLabel.frame.size.height)],
aLabel.frame.size.height);
}
+(CGFloat)widthOfTextForString:(NSString *)aString andFont:(UIFont *)aFont maxSize:(CGSize)aSize
{
// iOS7
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
CGSize sizeOfText = [aString boundingRectWithSize: aSize
options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes: [NSDictionary dictionaryWithObject:aFont
forKey:NSFontAttributeName]
context: nil].size;
return ceilf(sizeOfText.width);
}
// iOS6
CGSize textSize = [aString sizeWithFont:aFont
constrainedToSize:aSize
lineBreakMode:NSLineBreakByWordWrapping];
return ceilf(textSize.width);
}
then to use this, set the label's text:
label.numberOfLines = 0;
label.text = @"Everyone loves Stack OverFlow";
and call:
[AppDelegate fixHeightOfThisLabel:label];
Note: label's numberOfLines has to be set to 0. Hope that helps.
Solution 3
if you are using any of the system fonts, they changed in iOS 7 so they would be different sizes.
Also, sizeWithFont:constrainedToSize:lineBreakMode:
is deprecated in iOS 7. Use sizeWithAttributes: instead (if you are on iOS 7)
Solution 4
Accepted answer didn't satisfy me so I had to dig this up in my code:
CGSize possibleSize = [string sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:10] //font you are using
constrainedToSize:CGSizeMake(skillsMessage.frame.size.width,9999)
lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = label.frame;
newFrame.size.height = possibleSize.height;
label.frame = newFrame;
Solution 5
The accepted answer is too long. You can use the following:
+(CGSize) GetSizeOfLabelForGivenText:(UILabel*)label Font:(UIFont*)fontForLabel Size: (CGSize) constraintSize{
label.numberOfLines = 0;
CGRect labelRect = [label.text boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:fontForLabel} context:nil];
return (labelRect.size);
}
Related videos on Youtube
Sumeet Mourya
I have professional knowledge of development in most of iOS Frameworks in all iOS layers (Cocoa Touch, Healthkit, Core Services, Map kit, Bluetoothkit and UIKit), UITesting, Unit Testing, JSON, XML, REST API, SOAP API, DB, sqlite, HTML, CSS. Experience with work on these tools: Xcode, instruments, zeplin, lean testing, flyspray, wrike, sketch, trello. Beginner of the Flutter SDK, for use my technical skills set for hybrid platform too. Using architectures: VIPER, MCV, MVP, CLEAN. Start practicing: RxSwift, MVVM. Third party libraries: Github, Fitbit, Facebook, Instagram, Twitter, Google, EstimoteSDK Automate the taking screenshot for application using fastlane framework.
Updated on August 17, 2020Comments
-
Sumeet Mourya over 3 years
I am using the this method for getting the height of the UILabel Dynamically:
+(CGSize) GetSizeOfLabelForGivenText:(UILabel*)label Font:(UIFont*)fontForLabel Size: (CGSize)LabelSize{ label.numberOfLines = 0; CGSize labelSize = [label.text sizeWithFont:fontForLabel constrainedToSize:LabelSize lineBreakMode:NSLineBreakByCharWrapping]; return (labelSize); }
With this solution I am getting the exact size of UILabel if my code is running on below iOS 8 but if I run my application on iOS7 then it is returns a different value.
-
Sumeet Mourya over 10 yearsI am also doing the same but the frame(here as you have newFrame) value are different for iOS6.1 and iOS7
-
Sumeet Mourya over 10 yearsI have try with this too other code I have tried but the result is same…..
-
Armin about 10 yearsWhere exactly are you using
maximumLabelSize
? This doesn't work. -
Rejinderi about 10 yearssizeWithFont is deprecated in iOS7.
-
Tchelow over 9 yearsGood solution. Also easy to implement.
-
Abdul Yasin almost 9 yearsPerfect answer. Worked for me. Thank you so muchhh
-
msmq almost 9 yearsCondition should be if (requiredHeight.size.height > self.certificateNameLabel.frame.size.height)
-
saraman almost 8 yearsUse CGSize boundingBox = [label.text boundingRectWithSize:.....
-
Awais Fayyaz almost 6 yearsswift version please
-
Preetam Jadakar over 4 years@AwaisFayyaz: you could have updated, but thanks I updated now.