How to set default height of the UILabel in objective-C? - ios

I want to set the (no text) height like the UIlabel have a line text 's height

With AutoLayout:
add a constraint of height to the label!
With no AutoLayout:
set the frame will work!

Use autolayout constraints. simply put a constraint on height but make sure to put other constraints too else constraint bugs will occur. to check thatits occuring...set the background color to grey and you will see the results.

Related

Multiline problem of UILabel embed in UIView

Here is the screenshot of UIView with its constraints.
It is located in a tableview cell
When I use UIlabel without UIView I can get it to grow multiline. But It doesn't work with UIView which I use for padding
I tried sth like
cell.layoutIfNeeded()
cell.label.sizeToFit()
cell.label.layoutIfNeeded()
cell.view.layoutIfNeeded()
cell.view.sizeToFit()
How can I make multiline work?
Make sure you did not set any required static height for the label nor the view itself.
Make the label (which is the subview of the view) edges equal to the superview's.
Last but not least, set the values of content hugging and content compression resistance priorities to those you need.
And btw. - If all you need is the grey background that visually wraps the label by some constant, the label doesn't have to be a subview of that grey background view, just set the view's edges to the label's ones and do some layout corrections to fit your needs.
1.Set background color from attribute inspector for Label.Don't use background view for label.
2.Set UIlabel's lines property to no of lines that you want.
3.Don't give height constraints.give only top leading trailing and width.
1- Set UIlabel's lines property to no of lines that you want, you can set it to 0, then the UILabel will automatically resize
2- Don't give height constraints, give only top leading trailing and width
3-return in heightForRowAtIndexPath: UITableView.automaticDimension (Swift 4.2)

AutoLayout fill width of UIViewController

I'm trying to get a UIView to fill the width of my UIViewController I've set the constraints as follows
and I'm getting a layout as follows in IB
What am I supposed to be setting to remove the margins on the view? I have a scrollview which I'm happen to have the margins on.
I also use left margin and equal width constraints but that could be taste. The other way would be to use a trailing constraint.
You can remove the margin in your constraint options. Otherwise you have to set it to -16 or something like that
It show that you have given constraints with margin. To avoid constraint with margin you can uncheck checkbox...

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

Expanding UIView as UILabel inside grows

I have a UIView which is a superview of UIImageView and UILabel. UILabel can have different size depending on the amount of text in it. So, I need the UIView to grow and shrink together with the UILabel but can not make it working.
The screenshot of how it looks is below:
As you can see the spacing above and below of the label are too big. In the .xib I set up top and bottom constraints on the UILabel to be just 10 but apparently these are somehow violated. UIView has top constraint to the frame above with the image as >=10, so that it could grow from 10.
You can see .xib below:
I tried to increase Content Hugging Priority of the UIView but it does not help. UILabel has just 4 constraints: top, bottom, left, right relative to its superview UIView. I have also tried to change Content Hugging Priority of UILabel relative to UIView but no success. The UIImageView is the actual border of the UIView that is shown in the picture.
Think you're missing:
View:
Constraint: Height >= 20
Label:
Lines: 0
Constraint: Top and bottom margins to the (super) View
Constraint: Height >= 10
Screenshot of a working example:
Tip: use the preview
So, in my case the solution was to set constraints of UILabel relative to the UIImageView and then set the Content Compression Resistance Priority to 250. Then everything started to work. Thank you matt! I opened your github project and found that Content Compression Resistance Priority of UIImageView should be 250 - this was crucial.
Set the label to .sizeToFit(), or in InterfaceBilder the number of lines to 0, and either you set the max width (prefererred width) or make >=/<= constraints. As my experience, you have to play a bit with constraints to make it work, the preview in assistant editor is very useful in that case. Then you can set the width/height of the superviews to be the same as the label and some to make it bigger. Hope this works.

Auto sizing UILabel when using Autolayout

I'm creating a xib using autolayout (NSLayoutConsraint). Is there a way to use sizeToFit or something similar to make a UILabel fit its contents?
I've tried setting a constraint of "greater than or equal to x" and tried setting the height to be below this. However, it keeps overwriting the height property. sitToFit doesn't seem to work when you have NSLayoutConstraints.
How can I get the label to size appropriately?
Make an IBOutlet for your width and height constraints, and use sizeToFit to change their constant property, and call needsUpdateConstraints
Using a "greater than constraint" for the height of the UILabel and
calling sizeToFit within viewDidLayoutSubviews did the trick for me
I did not need to call needsUpdateConstraints.
You also need to set the number of lines to 0 to allow an unlimited amount of lines or > 1 if you want a limit.
Also the property preferredMaxLayoutWidth needs to be set, which is automatically set to the views width by the interface builder.
You should try to lower your height constraint priority.

Resources