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)
Related
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.
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.
I have one UILabel and One UIView contains other subviews side by side in Storyboard. The UIView should butt up against the right edge of the UILabel(trailing constraint of 1), but I also need the UIlabel (the one on the left) to set it's width equal to it's content size unless it hits a max width. Visually:
|Label text| |UIViewWithSubviews|
And I need the following constraints:
1) Label should resize width wise unless it hits a max size and height is also dynamic that means when text reaches the maximum width then the word wrap will break the sentence to another line. and UIview will become vertical to UIlabel.
2) UIView should always be butted up against the right edge of label one in vertical center.
How do I set this up in Storyboard?
Thanks In advance.
Is this what you need?
Here is how i did in Interface Builder.
1) Set a width constraint on the UILabel that is "Less Than Or Equal" to the max width you want.
2) Set the number of lines for the UILabel in the storyboard to be 0. This enables the wrapping you are looking for. Don't set any height constraint at all.
3) Set up constraint to center the UIView vertically with the UILabel.
4) Your other existing constraints sound fine. Just make sure that the UILabel has a clear x and y position, for example by anchoring to the leading and top edges of the superview. And make sure the UIView similarly has sufficient constraints as needed in addition to the vertical centering and the leading edge alignment with the UILabel.
I am trying to add a Multiline UI Label as a header followed by a UITextView as the content body.
I have added the UILabel and UITextView as follows:-
The Label has number of lines set to 0, character wrapping and standard left, right, bottom and top constraints. I have also set a minimum height constraint but that does not seem to change anything. The UITextView below it has a top constraint to the label and left, right and bottom constraints to the view/layout guide.
The actual output is this:-
There should be 3 lines for this label but it is showing only 2 lines/
This label should have 2 lines and the text as "Tricyclic anti-depressants" but the last line is not visible.
Is there anything wrong with my setup/constraints?
Thanks
Do these two things:
Increase the number of lines of the label
Set the Height Constraint to greater than or equal
Make sure the top label's Vertical Compression Resistance priority is higher than the textview.
I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue.....
The even_venue.. label has 3 constraints for now:
Vertical space with eventt_title
a horizantal space with the leading of the superview
a width constraints which defines that the label width is always less than the superview.width.
What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?
This are possible steps which can create expandable UILabel
Set layouts for UILabel fixing its position
Set number of lines = 0
Set content vertical compression resistance to 1000 (this will allow text to push the label)
Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance
Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:
Vertical space with eventt_title
A horizontal space with the leading of the superview
A horizontal space with the trailing of the superview
Set UILabel's Line Breaks as Word Wrap.
Set UILabel's lines property as 0.
I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:
In the storyboard, place your label with the origin where you want it.
In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
In the code, set the text of the label using setText:
[self.myLabel setText:#"MyLabelText"];
This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.
I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.
I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.
AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.