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.
Related
I have a horizontal UIStackView which has two UILabel in it. First UILabel is multiline (two line) and other one is one line only. They both have default content compression and resistance priorities. My problem is that even there is a gap between labels, "best" word in first text goes second line. I noticed that first label doesn't goes beyond half of total width.
What I want is that second label should always show itself and first label should size It self for remaining space. If It can't fit to one line It should be two line. However, If second label is too short and first label is a long one but both of them can fit, first label should go beyond half of the width.
P.S I need to use UIStackView in this scenario because there are other cases. I know putting two label inside UIView may solve the problem.
UIStackView:
- Distribution: Horizontal
- Alignment: Center
- Spacing: 0
UILabel:
- Number of line: 2
- Line break: Word wrap
UILabel:
- Number of line: 1
View hierarchy:
Desired Result:
OR
EDIT: I calculate the width of second label and give width constraint. I think It solved my problem, I'll test a bit.
//Give specific width to second label to make first one calculate right number of lines.
if let font = UIFont(name: "Metropolis-ExtraBold", size: 15) {
let fontAttributes = [NSAttributedString.Key.font: font]
let size = (secondLabelText as NSString).size(withAttributes: fontAttributes)
secondLabel.widthAnchor.constraint(equalToConstant: size.width).isActive = true
}
To try and simplify...
Forget calculating any widths... what matters is the horizontal Content Hugging and Content Compression Resistance
Leave the left (blue) label at the defaults:
Content Hugging Priority
Horizontal: 251
Content Compression Resistance Priority:
Horizontal: 750
But set the right (orange) label to:
Content Hugging Priority
Horizontal: 1000
Content Compression Resistance Priority:
Horizontal: 1000
Results:
The only other issue would be if the text in the right-side label exceeds the full width of the view -- but you haven't indicated that you might need that much text.
You should set different horizontal content compression resistants for each of them:
For this case, the blue one(multiline) should have something less than the orange one(Singleline).
Assistive note: Multiline: 250, Singleline: 750
Orange Settings:
Blue Settings:
Note that I have set the line limit of the orange one to 0. You can set it to anything you like. But if you like to make it selfsize to anyhight taller view should have higher vertical compression resistant than 250.
First embed your multiline label inside a view and constraint the label to this view (top, leading, trailing, bottom). After that add your view with your multiline label and single line label to your stackView.
Then you can use .fillProportionally for the distribution of your UIStackView and the spacing value to specify the space between your two labels.
Make also sure to use Required (1000) for your horizontal content compression resistance priority on your right number label.
View-hierarchy:
Result:
With spacing between your labels:
I am working on horizontal layout where the cell's dynamically update when the text size grows. Currently I have a UICollectionViewCell whose estimate size is 100 and have leading and trailing margins set as 12.
However, its adding such constraint even when the text doesn't grows which leads to increase the original size of the cell as well.
I have tried lowering the priority of the leading and trailing constraints but it still grows 12 points each side even when text is not growing. Is there any way to not let the constraint get applied if the size of the text doesn't grows or shrinks lower than of the estimate size?
Our UITextField instances are using the "default" settings. They auto expand based on user input. We'd like to set them to a larger initial width so they don't have to increase their width. What is the correct way to do this to while maintaining an 'adaptive' layout? Or is this just contrary to iOS best practices and we should leave it as is?
To determine a view's size the autolayout uses two methods:
- (CGSize)intrisicContentSize //with
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis;
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis;
and
width and height constrtaints (size constraints)
//also the autolayot can use inner constraints, but here it is not important
If you have size constraints and intrisic content size with different values, you shout set priority for constraints and for intrisicContentSize.
In your case, you can set low priority for contentHuggingPriorityForAxis for horizontal axis. Set middle priority for width constraint. Set Hight priority for contentCompressionResistancePriorityForAxis for the horizontal axis.
After that, if text be smaller then width constraint, the textField will have width same to constant of the width constraint. Then text will have more width then constraint, the textField will grow out of the width constraint.
Also I recommend for you add constraint with "<=" relation for max width and required priority
I have a UILabel with no of lines = 1 and auto shrink set to a minimum font size of 9. The font size of the label is system 70.0
I have increased the content hugging priority to 1000. I did this assuming that the height of the label will fit itself to the text.
But still the UILabel does not resize itself to fit it contents. The height of the label is very large. I want the height of the label to just fit it contents.
Thanks.
The problem is you have given both leading and trailing space, so label will stretch itself to satisfy these constraints, As label has intrinsic size(i.e it calculates size based on its content), you just need to give constraint for x and y position
So, delete your leading and trailing space constraint, just have Align CenterX and Align CenterY constraints, these will be sufficient to give x and y position of your label and you will get desired results.
Edit - Adding Screenshot.
Also understand that here I have given trailing space constraint >= 10 so that label can resize itself according to its content.
Also if your are checking in iphone 6 screen then AutoShrink Minimum Font Size - 9 will not be fit for screen width, try giving AutoShrink Minimum Font Size - 6.
Result of above constraint -
1.For Long text
For small text -
I have a UILabel where I've set all needed constrains, like pin it to the needed sides of other views, set height.
But right now I want to set constraint for width, based on the text length.
How can I make it programatically?
I think I want to something similar to VFL #”H:|-20-[label1]-10-[label2]-15-“
so first label should have width based on it's text, and has a distance form the left size equal 20 pt
the second label should have width based on it's text and has a distance from the left side equal 10 pt and 15 form the right side.
as you can see it cut off second label, but I want to have padding like test label at the top.
What you want to do is set the label's preferredMaxLayoutWidth and set the numberOfLines to 0. Give the label a top and leading edge constraint, but nothing more. Now the label will expand its width based on its contents, up to the max layout width, and then will wrap and expand its height instead — automatically.