Label width not grown according to content of label - swift

label width is not increasing according to content in table view cell
Please check the attached image for detail

You need to put below constraints to the UILabel instead of leading Constraints.
Why this happens so ?
As the leading is not defined flexible, It will choose the constant value and therefore it will by default will leave space equal or greater from leading.
If we set leading with safe area it will increase width as the text expand till it reaches the safe area line.
Hope this works !!!!

Make constraint >= 20(some constant) to Safe Area and it should work fine.

Related

What's the proper way to align a strechable Text and an image to two ends in Unity UI?

I have an UI element that has a Text and an Image as its children. It has a HorizontalLayoutGroup component that enables Control Child Size and Child Force Expand.
I want the Image has a fixed size, and Text has strechable size controlled by the HorizontalLayoutGroup. So when the Image is set to inactive, the Text fills the whole space and when the Image is active, the Text shrinks a little bit in order to give space to the Image. Right now this part works good.
My second goal is to align them to both ends: the Text in the left and the Image on the right with space in between. But changing Child Alignment can't achieve this.
I tried the following solution:
Add LayoutElement both to Image and Text. On Text, enable Flexible Width and set a a value, on Image, enable Min Width and set to a value. Manually adjust the two values until it seems right.
This solution seems to work, but I don't know why. Is anyone familiar with this?What's the recommended way to do it? Thanks!
I worked it out. On the LayoutElement, treat Preferred Width or Preferred Height as Max Width or Max Height. Enable them, set the value the same with min value. One the other objects that you want to stretch, enable Flexible values. Then all worked as we want.

Auto Layout TextField. Center, top

I try to understand auto layout. I have a Text field with 3 constraints:
- Align Center X to: Superview
- Top Space to: Superview 50
- Width Equals: 150
I have a warning: Equal constraints may cause clipping
If I change to
- Width Greater Than or Equal 150
I have the warning: Width and horizontal position are ambiguous.
I am confused. In fact, those Constraints work well for a button or a label.
- Why is it different for a TextField?
- How to position a TextField horizontal center, top 50, and width 150?
(It seems that I have to add a leading and trailing Greater Than or Equal. That seems redundant to the Width Greater Than or Equal 150. That works to silence the warning but that seems a solution too complicated and redundant. is that right?)
It's because UILabel and UIButton adjusts its size according to its content. In this case, you need to specify a proper width or you can give a leading and trailing anchor with the desired padding.
Here's is how you can resolve your constraint issue in this particular case. Give the text field the following constraints:
50 points from the top.
Leading from the super view with padding.
Trailing anchor from the super view with padding.
The last two anchors can be replaced with these two constraints:
Width constraint for the text field.
Centre horizontally to the super view.
Here's how you achieve this on a story-board:
I just checked this on:
Xcode 11.2.1
Xcode 11.4.1
The warning shows on 11.2.1 but does not show on 11.4.1
In my view, even though it is a valid warning, it was a silly one and appears to have been removed in the version update.
If you want a label to have a width of 150, of course you know the text will be clipped if it is too long. There is no way around that for a single-line label, unless you allow it to expand outside the bound of the window.
Again, that's a valid warning, but that may be exactly what you want.
If you want a text input field to have a width of 150, the text will scroll when you input too much to fit.
So, why the warning on a text field? No idea. That probably could be considered a bug.
You can get rid of that warning by giving your Width: 150 constraint a Priority: 250. Just single, quick test shows that the field width will remain at 150 even when typing too much text. But... I'd hate to rely on that assumption, and if the field width / leading / trailing etc is related to any other UI elements, I wouldn't count on it not causing other layout issues.
I would say (my opinion only) this is an IB warning that is safe to ignore.

UILabel: Get actual font size with adjustFontSizeToFit

I have two labels in my layout.
I want the font size to adjust to fit the label bounds.
I set to true the UILabel adjustFontSizeToFit.
Moreover a need the two labels to have the same font size. So I added an auto-layout constraint to make the labels have the same size.
The issue with it is the labels do not contain the same string. Sometimes the first is longer and vice-versa. The font I use doesn't have the same width for every character which will not allow me to use a space to fill to shorter string. (This solution looks a bit ugly to me).
I tried to get the label.font.size.pointSize after the label has been displayed but the value returned is always the one I set at the beginning (before being reduced to fit).
Does anyone have an idea on how I could achieve it ?

Width limitations with constraints when window resized

I am trying to add a custom view in an nswindow in my osx app.
I need to give a minimum and maximum width values for the custom view which is located in the centre. The view's width should expand until a certain point (maximum width value) but should stop expanding if user continues to expand the window.
Thanks in advance.
You can do all of this with layout constraints.
First, we need to specify where the view should be relative to the window. For the sake of this tutorial, I'm going to assume you want it centered:
Next, we add our constraint for the minimum width:
To make this a minimum instead of an absolute width, click on the constraint and change it to "Greater Than or Equal" in the Attributes Inspector:
Now do the same thing, making another width constraint for the maximum. This time, set it to "Less Than or Equal":
Now the width constraints are set up. But we're not done. We've now set a minimum and a maximum, but the width is still ambiguous—there is no way for the layout constraint system to decide what exact width between 300 and 700 that it should actually be using at any given point. There are two steps to fix this. First, we need to make sure that the view will be entirely within the window and not run off the edges, so create some Greater Than or Equal constraints making sure it stays within its bounds:
(Also, make a trailing constraint which is set up identically).
Finally, we need one last set of constraints; we want some leading and trailing constraints, marked Equal, but with a lower priority:
(Also add a trailing constraint, identically configured)
What does this one do? Well, it tells us that, unless our other constraints (specifically the maximum width, in our case) make it impossible, we'd like the edges of the view to be the standard distance from the window edge. The reason we use 499 as the priority is because that the value of NSLayoutConstraint.Priority.windowSizeStayPut is 500. The documentation has this to say about .windowSizeStayPut:
It's generally not appropriate to make a constraint at exactly this priority. You want to be higher or lower. Constraints with higher priorities can adjust the window’s size. Constraints with lower priorities must be fulfilled using the current window size.
If we set our constraint to higher than 500, the system would restrict us from making the window too wide for these constraints to be valid. That's not what we want, since we want the edge spacing to expand in this case. So since we want to be able to break this constraint by resizing the window, we set it to slightly less than 500—so, 499. This means that the constraint system will try to put the view here, but if it can't do it because we made the window too wide, it will allow this constraint to break, although it'll still try to get as close as it can without breaking the other constraints. So your view will be at its maximum width, and centered in the window.
Voilà!

UILabel Text Not Wrapping

I am working on a Swift project with Storyboards where I want text to wrap in a label. In the old Objective-C version where I did not use a Storyboard I used the following settings and things worked perfectly.
Here are the settings for Swift
I have been reading about the potential auto layout issues with preferred width settings. I currently have them set to auto layout and the label itself is set to a width of 560. I've added a constraint to keep the label 20 pixels from the trailing superview and while I thought this would work I still cannot get the text to wrap. The dimension settings are below.
Can someone explain how to get the text to wrap?
First, the good news: You have set the label to 2 lines and Word Wrap. So it can wrap. Excellent.
Now you must make sure the label is tall enough. Either give it no height constraint, or give it a big enough height constraint that it can accommodate two lines.
Finally, you must limit its width. This is what causes the text to wrap. If you don't limit the label's width, it will just keep growing rightward, potentially continuing off the screen. The limit on the label's width stops this rightward growth and causes the text to wrap (and the label to grow downward instead).
You can limit width in several ways. You can have an actual width constraints. Or you can have a leading constraint and a trailing constraint, to something relatively immovable, such as the superview. And there is a third way: on the Size inspector (which you do also show, at the bottom right of your question), set the Preferred Width (it is shown at the top of the Size inspector): this is the width at which, all other things being equal, the label will stop growing to the right and wrap and grow down instead.
Declare your UILabel programmatically and give
yourUILabel.contentMode = .scaleToFill
yourUILabel.numberOfLines = 0
yourUILabel.leadingMargin(pixel: 10)
yourUILabel.trailingMargin(pixel: 10)
This worked for me.
Your text will wrap if you have provided lines number more than 1. However you may not be able to see it wrap if the label height is not enough to show the content. I suggest you to remove the height constraint or increase its value.
In case this helps anybody: I had followed the advice given here to fix my label not wrapping to two lines but nothing worked. What worked for me was I first deleted some of the relevant constraints in storyboard (I'm using auto layout) and saw that the label wrapped properly. I slowly added back the constraints I needed and everything still seems to work fine. So deleting and remaking your constraints may help.
What fixed this problem was changing the label type to "Placeholder" under Intrinsic Size in IB. When I changed this the text wrapped and the warnings went away.
As I see you interface builder. There are two problems. First one is with your constraints, and another one is with the property.
You gave it a fixed height which is wrong while line wrap. You need to make the auto-resizing label, i.e. remove height and add the bottom constraint or simple remove height depend on your situation. Your text is moving to the next line, but due to fixed constraint, you can't see it.
You enable the option to clip subviews which is wrong as it cuts your view and you are unable to view wrap word.
Add a new case:
DO NOT add constraints to your label with a TEXTVIEW, or the label will expand to right without limitation.
In my case i set my parent stackview alignment from center to fill and set UILabel to
label.textAlignment = NSTextAlignment.right