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.
The accepted answer is too long. You can use the following:
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)this code is for moving four labels clockwise with the button tap, by using function for button:
Super simple. Just get the area of the text, divide by width, then round up to the nearest height that will fit your font.
Very much a plug and play solution. I use it in a helper class a lot, especially for dynamically sized UITableViewCells.
Hope this helps others in the future!
I have a situation where i need to set the height of label dynamically according to text. I am using Xcode 7.1 and my project deployment target is 7.0 but i tested it on iOS 9 simulator and following solution works for me. Here is the solution: First of of you will create a dictionary like this:
now we will calculate the height and width for our text and pass the newly created dictionary.
Then we will set the frame of our LABEL:
THIS IS HOW I SUCCESSFULLY SET THE FRAME OF MY LABEL ACCORDING TO TEXT.
Here's a total solution for width and height. Put these in your AppDelegate:
then to use this, set the label's text:
and call:
Note: label's numberOfLines has to be set to 0. Hope that helps.