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.
Accepted answer didn't satisfy me so I had to dig this up in my code:
Whatever height I am getting via this code(method I have written in this question above).Its provide the height in float value (86.4) , once we get that and try to set that height to UILabel, but we need to get the value from the height with ceil (87) instead of the value as it is (86.4). I have resolved my problem with this approach. And thanks for your answers.
I all the time use
sizeThatFits:
You can try this
This method is used and tested by me from iOS 7 to iOS 11.4
So you can use like this:
You have to dynamically set the frame, like below:
This is what I came up finally and hope this will help you. I checked iOS version as Apple itself doing in the iOS 7 UI Transition Guide, which involves checking the Foundation Framework version and used #pragma to suppress the Deprecated: warning raising by iOS 7 or later with "- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size".