How do I find the line height of a UILabel? - ios

I have a button placed beneath a label and want to make it look like it's a continuation of a label. I think I can achieve this effect by changing the height of the button to match the UILabel's line height.
How do I get the line height of a UILabel?

You can grab the height off of the UIFont:
label.font.lineHeight

Related

How can I set a multiline UILabel inside UITableCell on Swift 3?

I am trying to set a multiline UILabel, inside an UITableCell.
That UILabel has a proportional width taking as reference the screen of the device (with a multiplier). The text inside of it can change so I need to fit on the UILabel being able to multiline when the width of the text is higher than the space of the UILabel width.
I tried using:
myLabel.lineBreakMode = .byWordWrapping
myLabel.numberOfLines = 0
myLabel.sizeToFit()
but it is always displayed on one line truncating the text that overflows the UILabel width.
How can I make my UILabel to be multiline?
EDIT: I cannot put breaklines to my text because I retrieve my texts from my database.
Thanks in advance!
These two lines are necessary for calculating the cell height automatically. Put these in viewDidLoad.
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
If you want a label to switch from one line to multiple lines, you either have to specify line breaks in the text of the string with \n or newline char, or you can set constraints for the width of the label, or you can set leading and trailing constraints for the label. If one of those methods is complete, the label will go to as many lines as it takes to hit the max number of lines or infinite if set to 0.
Finally I was able to make my multiline UILabel adding this code:
self.tableView.estimatedRowHeight = 50.0
and reducing the multiplier value. It seems that the text could not fit on the height of the UITableCell.

UILabel height calculation

I have UILabel that the text is dynamically change based on server data. Sometime the data is so long that make my UILabel become multiline. Is there any way to calculate the height of my UILabel?
You can use this :
var labelHeight : CGFloat
labelHeight = theLabel.boundingHeightForFixedWidth(theLabel.bounds.width)
Hope it help :)
If you are using Autolayout then no need to calculate height, just add leading,top and trailing constraint, set number of line to 0 for that label and line break mode to word-wrap, it will automatically update its height.
P.S. Add the bottom contraints too if there is any other control in nib after label.

Multiline Labels inside scrollView is shown only in a single line and expand to its full width

I have UILabels inside UIScrollView. My labels can expand to more than one line. i have set myLabel.numberOfLines = 0 because it can expand to 2 or 3 lines. It work fine when it is not shown inside UIScrollView. but when i put it inside UIScrollView, this label is shown as one lengthy label and not breaking to multiple lines. instead horizontal scroll is shown. I don't want to show horizontel scroll, instead i want my lables to expand to more than two lines and cover only the width that is available.
You have to set Content size of scrollview and set the frame of label... It will definitely work..
you have to set new size of the label according to line increase or decrease like this.
-(NSInteger) linesCount {
CGSize newSize = [self.lblstring sizeThatFits:CGSizeMake(self.lblstring.frame.size.width, MAXFLOAT)];
return newSize.height/self.lblstring.font.lineHeight;
}
so that you get that how many lines of label you have to do....

How can I know the height of a superview after I change one of its subview like UILabel

I change the My UILabel's text and make it become multiple lines. I guess the the superview of this label will resize after that. But how to get the new frame of the superview as soon as I change the label.text?
CGSize size = labelSuperview.size;

How to adjust UIlabel

I read this answer in how to adjust the height of my label based on the text of the label:
Vertically align text to top within a UILabel
This is my code trying to set new Text and adjust the label at the same time.
But what I find out is the height of the label never get changed. It ways stays at the value I set in my xib file. I use tool, Spark to inspect the height of the label:
-(void) setNewText(NSString *)newText
{
self.myLabel.text = newText;
self.myLabel.numberOfLines = 0;
[self.myLabel sizeToFit];
[self setNeedsLayout];
}
Can you please tell me what's wrong with my code?
Thank you.
Updated:
I have tried #rdelmar suggestion. Set the width constraint for the label and num of line to 0. The height of the ui label does get adjusted. But there are extra spacing before and after the first/last line of the text.
Here is the screenshot I captured using Spark. The blue rectangle is the UILabel. As you can see, there are extra spacing before/after the first/last line. How can I remove those spacing?
You don't need to do anything in code to get your label to adjust its height. In the xib or storyboard, give the label constraints to set its position, and give it a fixed width constraint. Do not set its height. Set the numberOfLines to 0, and that's all you need to do. If you set the text with a string that's too long to fit on one line, the label will adjust its height automatically.

Resources