how can i prioritize a label to be displayed fully and another label to display it too but if its too big it should make ... in the UIStackview
This is my StackView
Here is an example that follows what you want:
First, a UIStackView will actually make things harder here because we want to fine-tune the distance between each label. I just used leading and trailing constraints to layout each label in a line.
The trick is to set the proper content compression resistance priority and content hugging priority for each label. First, we want 0s to always match the size of its content, so we set its content compression resistance priority to 1000. Content hugging priority can stay at the default of 250.
I also split the # sign into its own label, since we always want it to appear. Set its content compression resistance priority to 1000 and keep the content hugging priority at 250.
Next, for the shrink_label, we set the content compression resistance priority to 749 and the content hugging priority to 1000. That is, we want the shrink_label to never try and grow bigger than its text content size, and we want it to allow its content to be compressed if we need.
Finally, for the laaabel we set the content hugging priority to 1000 and the content compression resistance priority to 750. That is, the label will take up as much space as it can, and since its compression resistance priority is 1 higher than the shrink_label's, the shrink_label will compress before the laaabel.
You can read about content compression and content hugging in the Apple docs here:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/ViewswithIntrinsicContentSize.html
The full source code for my example app is available here:
https://github.com/elliotfiske/StackOverflowContentHuggingExample
Related
While learning content Hugging priority I came up with the weird scenario, I have taken 2 labels, 1 Green and 2 Blue.
Content Hugging Priority of these labels are like
Green - Horizontal hugging priority 200
Blue - Horizontal hugging priority 251
"
Green.text = "Hello There"
Blue.text = "How are you?, have a good day, big text
Here Green labels text is truncated and blue will show full text.
Now Problem is when I try to revers the priority of both labels and text then it is not working same as above.
New Priority is now
Green - Horizontal hugging priority 251
Blue - Horizontal hugging priority 200
Green.text = "How are you?, have a good day, big text
Blue.text = "Hello There"
Now I thing Green should show all the text rather than truncating it and blue should be truncated. but its not happening so, I want to know why this is not working?. am I missing or miss interpreting this concept?
Please correct me if I am wrong. Thanks
You need to increase Content Compression Resistance Priority of green label.
Try selecting "Fill Proportionately" in the dropdown-menu for your Stack View's (Attributes Inspector) "Distribution" category.
Make sure you're selected on the Stack View.
Click Attributes inspector menu
Click Distribution and select "Fill Proportionately"
Not sure if you even need to do anything with each element's "Content Compression Resistance Priority" or "Content Hugging Priority" when doing it this way; you can try it with and without (an element with a lower Compression Resistance setting allows element to be compressed, and higher Hugging Priority number means the element will tend to grab more space) and see if it does anything different.
For me, Fill Proportionately took care of the whole problem, and I adjusted the two Priorities mentioned above back to neutral (each element have same number). Before this image shot, "Baseball Sunsetters" was truncated as both it and the numerical element ("1") were taking up the same amount of horizontal space in the stack view. After (Photo): the Name Label shifted to the right and Members Label compressed (without using compression or hugging priorities)
There are times when Content Hugging works pretty well, but look at its documentation for more details.
I have two labels side by side. And I have two constraints set. Label-A starts at the leading edge of the view. Label-B starts 10px from trailing edge of Label-1. There are no width constraints.
Label-A's text length varies; Label-B's text is fixed (6 chars long). This works fine until length of Label-A goes over a certain point. Then Label-B goes off screen.
Before I assign the text value to Label-A, I want to truncate Label-A text with '...' if it will push Label-B beyond the trailing edge of the view. Any idea how to figure out if I should truncate Label-A's text and by how much? This has to work in both portrait and landscape modes.
Thanks.
Set Content Compression Resistance Priority for one of both label, according to your requirement.
Look at these snapshots:
Two labels with default content compression resistance priority.
I changed content compression resistance priority for label blablabla blablabla, from 750 to 749.
Result is:
For more details see Apple document: Setting Content-Hugging and Compression-Resistance Priorities
Make the horizontal content hugging priority and horizontal content compression resistance priority of Label-B higher than Label-A. And also add trailing of Label-B to superview.
First of all make sure that your labels have constraints for leading and trailing.
Your views layout should like this:
|-8-[Label-A]-10-[Label-B]-8-|
After that set the horizontal content hugging priority and the horizontal compression resistance of Label-B higher than Label-A.
I am showing two labels in a stackview as shown in the image below. I would like the first label to show its full content. For this, I tried making its horizontal content hugging priority as well as its horizontal content compression resistance priority higher than the other label. Still i am getting the cropped label as in the image. Isn't this supposed to work? How do I get the desired result? The stackview's alignment is set to 'Fill' and its distribution to 'Fill Proportionally'.
Set the distribution to “Fill”, not “Fill Proportionally”.
I have a bar with two UILabels:
[LeftMsg RightMsg]
The rules I want to set are:
RightMsg is always fully visible, right-aligned and takes the room it needs.
LeftMsg is left-aligned and takes the remaining room.
For example, if LeftMsg reads "This very long message does not fit the bar", it must be displayed as follows:
[The very long message does n... RightMsg]
I set horizontal auto-layout constrants as follows:
LeftMsg.leading = Superview.leading
RightMsg.trailing = Superview.trailing
LeftMsg.trailing <= RightMsg.leading
(If I use equality in the last constraint, XCode tells that there is a content priority ambiguity).
Now it works as follows:
[The very long message does not fit the...]
that is not what I need.
Can anyone suggest how do I correctly set constraints to achieve what I need?
I think you can set lower horizontal hugging priority for your left label than that of right message label, while at the same time setting higher horizontal compression resistance priority for right message label than that of left message label.
For example, you can set both Content hugging priority and compression resistance priority for your labels like this.
Left Label : content Hugging priority ( H: 250, V: 251)
compression resistance priority ( H: 750, V: 750)
Right Label : content Hugging priority ( H: 251, V: 251)
compression resistance priority ( H: 751, V: 750)
You can see more information about content hugging and compression resistance in this tutorial.
Set the content Compression resistance property of the left label to 750.
Set the Content Hugging Priority of the right label to 250.
Please follow this,
First set both label on View.
Set constraints for View is Top, Bottom,left and Horizontal center
align.
Set constraints for left label is Top ,Bottom,Leading and
Trailing.
Set constraints for Right label is Top ,Bottom and Trailing.
Select both labels and set Equal height and Equal width.
Finished....
i have a UITableViewCell that contains some UILabel and i want that this label fill horizontally the width of the UITableViewCell and that doesn't remains big space instead of other, this is an example:
|---------------------------------------------------------
| |
|***This is a uilabel***This is a uilabel***label***lb** |
| |
|---------------------------------------------------------
So with different UILabel width i want that all the UILabel fill all the UITableViewCell fixing the font size if needed, the * is the spacing, how i can achieve this with auto layout?
STEP 1: Set up constraints
Leftmost label: Add a leading edge constraint and a center Y constraint to its superview
Middle labels: Add a horizontal spacing and center Y constraint to previous label.
Rightmost label: Add a horizontal spacing and center Y constraint to previous label plus a trailing edge constraint to superview.
These constraints ensure the gaps between the labels and on the left and right are fixed in size, and that they are all centered in their superview. Because there are no width constraints, the layout system will attempt to size them to fit their content. But this leaves an ambiguity...
STEP 2: Set content size priorities.
The above constraints are still ambiguous -
If the total text content is less than the superview's width, which label will expand bigger than its content?
If the total text content is more than the superview's width, which label will contract shrinking its content to fit?
The answer to the first question is the label with the lowest horizontal content hugging priority. By default they will all have the same, so make sure they have different horizontal content hugging priorities.
The answer to the second question is the label with the lowest horizontal content compression resistance priority. By default they will all have the same, so make sure they have different horizontal content compression resistance priorities.
STEP 3: Ensure text shrinks to fit.
We now have an unambiguous layout. But by default, the labels will truncate their content if they do not have room to display it. Set Autoshrink to Minimum Font Scale or Minimum Font Size on your labels to ensure they adjust font size to fit their content rather than truncating it.