NSLayoutConstraint constraintWithItem set width based on text size - ios

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.

Related

iOS: Correct method to set or increase text field width

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

Label Following Device Size

Just simple question.
How to make the size of the UILabel following device size?
for example, I have 1 label that have size 20 running on iPhone 5. Then if running on iPhone 6 the label size changed to 40. What should I do?
If you are using autolayout and you want to increase height and width of label with respect to screen size then you should simply give four constraint to label like : top,bottom,leading and trailing
If you want to increase height and with with in specific desired value, for example if you want label width exact half of screen width, In this type of case you should give constraints like : leading,top,fix height, fix width. Then, select fixed width constraint and from size inspector change it's multiplier to 0.5. so your label width always remains half of screen width. you can do same for height. and you can set different multiplier value to get desired output.
Hope this will help :)
Normally I will use ratio to define the constraint. For example, if your label height design is 40 px for iPhone 6, I will define the label constraint by setting equal height of the label and the main view. After that change the multipler of the constraint to 40:1334. The label will have exact same ratio of the screen.

set label position with priority constraint?

I have one label to the right side of parent view.On top of label there is a view.I have given below constraints to label.
Align leading to top view (constant<16) LOW PRIORITY
Top space from parent view
Trailing space to parent view (constant=28) HIGH PRIORITY
I want when text inside the label increase it should change it's position.it should move towards left but at the same time it should also move towards right.I mean should increase it's width in both directions.As of now it's only moving towards left & keeping fix position from right.
Please tell how can i achieve this task?
i think if you set center alignment for label alignment you should get answer you want.Also give same priority to leading and trailing.
This will not work as the trailing space to super view constraint will most likely always win, as you have observed.
The width and height of the label is taken care of by its intrinsic content size and hence no constraints are needed for that. As far as position goes, the y is taken care of by the top space constraint. Finally the x will need a constraint that will allow it to grow in both directions as mentioned. To enable this behaviour remove the leading and trailing constraints and add a horizontally centered constraint. This will centre the label to its super views centre. To offset the position left or right you may choose a constant value or use the multiplier property.
The multiplier is good for positioning a certain percentage of the superview regardless of its size. e.g.: if the superview is the root view then a multiplier value of 0.5 places the label quarter of the way across the view, 1.0 makes the centre x align exactly, 1.5 makes it 3/4 across from the left.

UILabel Autoshrink Mal-use?

I Need a label in my UI to hold up to 32 characters without distorting, so I enabled auto shrink and changed the number of lines to 0:
heres my label on the IB:
now when I run the application it runs off the screen:
how can I make it fit accordingly?
You don't want the number of lines to be 0, because that will tell the label to use any number of lines that it needs. You also have to constrain the label's right edge to its superview's right edge, because without that, it'll run off the right side, as you can see.
remove constraints first
give the width 0 and apply width constraints . Click on the constraints change width constraint = to >= after which side you want (left or right) apply top space and (trailing space or leading space) apply two constraints.
clear the lable text.
try it and let me know after this wat is th problem.
You are on right direction. Set minimum font size and maximum font size of the label and not need to truncate characters, not need to set number of lines. Autoshrink definitely works.

Text alignment from bottom to top iOS SDK

As seen on the screenshot above, each top cell has few labels. The frame of the label this question concerning is highlighted with green rectangle. It shows the title of some picture/sculpture or other exhibit, so there could be quite long strings.
I would like the text of this label to be just above author label (string with font of smaller size). So, normal text is aligned from top to bottom and if text is not enough to fill all the space, the gap is at the bottom part of the label.
For this label I would like to use the opposite approach: if text is not enough, all the text at the bottom. Is there any parameter of UILabel to implement above mentioned behaviour? Or I should manually count the amount of lines and adjust the size of the label?
This is fairly easy using constraints. To get the effect you desire:
Constrain your label's position above the author label
Constrain the label's width or horizontal position
Constrain the label to a reasonable height using a greater than or equal to
Either constrain the label's total height as less than or equal to, or constrain the vertical position to something greater than or equal to.
All these constraints can be added in IB very easily, or through code. These constraints will cause the label to grow vertically to accommodate more text, and then shrink back down with less text, which will achieve the effect of having the text grow up from the bottom.

Resources