Make label height as content in it without spacing? - ios

I have added a label on storyboard. I have given it line as 0. It's height is not static. But when there more lines then it height increased but space is added from top & bottom . I want to remove those extra space when label height is increased. I have tried using label.sizeToFit() but it align text to the left & I want text to be in centre. Only extra spacing from top & bottom should be reduced.

Here I tried following steps with SafeAreaLayout/AutoLayout to solve this problem.
For label instance in storyboard (view controller) or programmatically
Remove all (existing) constraints applied on your label.
Set all constraints (top, bottom, right left) again, with respect to its superview/adjacent UIElements.
Set number of lines = 0 (& line break mode = truncate tail)
Here is result:
Edit: Answer to your query -- my label is vertical centre to a super view
Remove Top constraint for your vertically centered label and apply constraint: Vertically in container = 0
Result with update (Vertically Center):

Edit : Make sure to decrease font size as when font becomes large , intrinsic add more space around it
contentHuggingPrioprity vertical set it to 1000

In addition to sizeToFit you could set
label.textAlignment = .center
You could also add an UIView with constaints set like the label is set right now. Inside that View you add the label with constraints to top, left and right and the height to be greater than or equal to 0.

Related

How to horizontally align two UILabel (with fixed and variable width)

I have been facing issues horizontally aligning two UILabel and one UIImageView like this:
First label has variable width, can be truncated if long. Second label has fixed width, it should always be aligned to right of UIImageView. It should never go off screen. UIImageView is aligned to right of first label.
I have tried embedding them in horizontal UIStackView but the image + second label always aligns to end of cell. Got the same issue when trying without UIStackView.
Please help.
You can embed both label and horizontal StackView into another horizontal stack view. Then, you'd need to set the dynamic width Label's Content Compression Resistance Priority (you can find this property at the bottom of the Size Inspector), to be smaller in order for it to shrink.
Then on the container StackView (the one that contains all views), you'd need to set constrains to top, bottom, leading to 0 to the superview and the trailing to be greater than or equal to 0, for it to not take all space of the superview, but at the same time not get offset if the content is too wide.
I hope that is clear enough!

Positioning content relative to something else

I have three labels in my view:
The first label is a description and as the text will be fetched from an API it varies in length.
First question is how do I make the description label auto-adjust its height?
Second question is how do I make label 1 move relative to the height of description label and also label 2 relative to label 1.
You change the number of lines to 0 and line break mode to word wrap to make a label arbitrarily tall. You embed all three labels in a vertical stackview and set the spacing in the stackview to distribute them with equal spacing. Constrain the stackview to the top, leading and trailing edges of your view. You do not need to give it a height as it will derive its intrinsic height from the labels plus the spacing.
you should definitely take a look at autolayout:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/
How to auto adjust height
descriptionLabel.sizeToFit()
How to auto adjust height in relation to another label
label.frame = CGFrame(x: decriptionLabel.frame.origin.x, y: descriptionLabel.frame.origin.y + descriptionLabel.frame.size.height, width: descriptionLabel.width, height:0)
label.sizeToFit()
note: You might want to add some padding to the y co-ordinate otherwise it will literally start where the description label ends.
You can do all what you want to do without writing a single line of code.
To answer your first question : For the description label set number of lines to 0;
To answer your second question You can do it with 3 simple steps using auto layout :
A) Description label: set constraints to : TOP to superview , leading space to superview , Trailing space to superview, Bottom space to label 1.
B) Label 1 : leading space to superview , Trailing space to superview, Bottom space to label 2.
C) Label 2 : leading space to superview , Trailing space to superview.
Auto layout will adjust and move desire elements based on contents.

ios auto layout label positionning

I am working with the storyboard using auto layout.
I need a particular layout and I am not able to find a way to do it.
I have two UILabels, the first one regular and the second one bold (That's why I used two UILabels).
I need the second UILabel to be near the second. But if the second label does not have enough space to displayed, then I want it to wrap the text.
In your storyboard, set property of textfield as lines=0 give below constraints only
Top leading trailing to supverview
Height with relationship greaterthanequalto
Note: don't give bottom constraint to label
To make some part of label bold use NSAtrributeString. Don't take two labels.
Case I : If you want to adjust font size in the same line then,
Vertical space from SE CONNECTOR button
Leading constraint to superview
Trailing constraint to superview
& add code to adjust font,
yourLabel.numberOfLines = 1;
yourLabel.minimumFontSize = 8;//Keep min font size you wants
yourLabel.adjustsFontSizeToFitWidth = YES;
Case II : If you don't want to adjust font size & achieve it in multiple lines then,
yourLabel.numberOfLines = 0;
Vertical space from SE CONNECTOR button
Leading constraint to superview
Trailing constraint to superview
Give Greater than equal height constraint(& set constant to 0)

UILabel keeps resizing to a height of 0 regardless of the text

I have a UILabel with the following constraints
and I set the number of Lines equal to 0. The layout on IB can be seen below. I want this UILabel to expand dynamically based on the text that I receive where the views below it get pushed down based on the height of the UILabel. However, that is not what happens.
As seen below, the UILabel doesn't appear at all. It seems to have a height of 0 regardless of what I set the text to be. Does anyone know what I need to modify to make this happen? Does anyone know why the UILabel has a height of 0?
UPDATES:
Things I tried given the comments below.
word wrap, nothing changed
adding a height constraint on UILabel, text gets cut off after the first line
height constraint with greater than or equal to constant, text still gets cut off after the first line
I created a dummy View according to your requirement.
Button has fixed constraints : leading width ,height, top space.
For label : leading, trailing, topSpace to button
3.For View below the label: give leading, trailing, height , top space to label
so here label height is not fixed .so it will change according to text.
After setting the text in label try out "labelName.sizeToFit()" this will automatically adjust the height of your label.
You should try to set a height constraint to your Label.
Considering the information you provided, it seems like it doesn't have enough height because you are using bottom constraint to the view under it. Rearrange your constraints or give a height constraint to the UILabel.
Select the UILabel,Set the Line Breaks mode is Word Wrap and set the number of lines is 0, example is given below,
And set the Height Constraints, If you set height contraints then select the UILabel, see the right side, select show the size Inspector --> Double click the height Contraints --> Relation --> set the Greater Than or Equal, its automatically expand the label, example is given below,
hope its helpful

UILabel implemente autoheight with center placement and a button to the right using AutoLayout

I want to have a UILabel with a button to it's right centered horizontally like of the picture.
I've implemented an "Align by Horizontal center" for the UILabel and standard space between label and button and it usually works just fine.
The problem appears when UILabel's text is very long and it goes out of bounds and the button is not even visible.
I've tried to set lines count for the UILabel to 0 and set questionLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.questionLabel.frame)what makes the label to grow vertically. But it's width is very small (it's the base width from ui designer) so it looks like a column of text.
So the question is how to make UILabel to:
to fill all available horizontal space without hiding the button
autoscale vertically if there is no enough horizontal space to fit text into one horizontal line
be centered horizontally
It's missing the constraint from the trailing of the UIImageView to the trailing of your container view or margin. I used a Greater Than or Equal constraint so if the label has a short length, the image will stay close to the label - using the horizontal space constraint from the label to the image.
With that, your label - that is centered relative to your view, will be centered and will respect your trailing constraint of the UIImageView to the view using all the horizontal space that is possible to use as follow:
In the following image it's possible to see that the label reaches the maximum width it can, according to the constraints:
you could remove the width constraint and add a horizontal space constraint from the right side of your label to your image and one from the left side of your label to the left border of your view (set the constant to the minY Value of your Image to get it centred)

Resources