How to resize label proportional to its superview - ios

I have a view which contains a label (one line label). The label its centered horizontally in the top part of the view (20 from the top). When this view is resized (for example for iPhone 5 screen) I want my label to resize as well.
The label font has size of 55, when the view is resized the font remains the same and it looks to big.
Currently my constraints are just centered horizontally and 20 from the top.
How can I make my label to resize proportional to its view ?

As I understand it you need the font to scale?
You can use Minimum Font Scale beside Autoshrink in the Label's properties.
Just set your label's font to the maximum size you want it and set Minimum Font Scale to the mimimum size you would like that font to be at it's smallest.
For example if you leave it to the default value of 0.5 the font would shrink to half of it's original size.
Then you need to tell your label to stretch proportionally to the view.
Simply add Leading and Trailing constraints to the label and it will scale it's boundaries when the superview resizes.
The font will scale until the text can fill the boundaries of the label using the font size as maximum and Minimum Font Scale as minimum.

Another easy way is using Cartography for auto layout.
constrain(labelView, yourSuperView) {
view, view1 in
view.top == view.superView.top
//...
}

Related

how to make the label proportional to the screen size?

I am a beginner, I am trying to do an autolayout. I am trying to make the label 'HRIS' and 'Please login to continue' to be proportional to the screensize (its superview), I can do it for the login button and the image leaf as the picture above, we can see that the login button and leaf image are proportional.
for the image leaf, I do equal width with 0,1 multiplier to the superview, and then I also add 'aspect ratio' to make it proportional.
but when I apply the same way to the label, it doesn't work, as we can see the label size still the same for iphone 4s and iphone 8, maybe it is because the instrinsict content size (the font size itself) that makes it like that. I tried to apply this way, but it seems the size is still the same
I want to make it little bit smaller for iphone 4s, I also can't modify using size classes since it is in the same class
so what I have to do to make label proportional to the superview (screen size / background) ?
You can do it by setting both proportional width of the label and allow it to scale down the point size.
Set the label width to proportional width like this. It is just a regular same width constraint that you edit.
Then allow your label to scale down the point size like this. This is a property of your label.
Set different font size using variations...
Simply set up a constraint for your label's width to be equal the view's width with your desired multiplier. In my example I used a multiplier of 0.5:

How to increase the font size in propotion to view height

I am increase Textview height in propotion to parent view but font size remains same.
I would like to change the font size accoring to textview or screen size so it fills up the space in textview
The problem I am facing is in iPhone 4s it fills up the space while iphone 7s there are lots of empty space
is there any constraint we can use to achieve that other than doing programtically.
If you use a UILabel instead of a UITextView, you can set the Font Size to very large, with a small Minimum Font Scale. Then, your text will "auto-size" to fit the view.
In this example, the Font is set to System 100.0 --- really, really big. Autoshrink is set to Minimum Font Scale with a value of 0.1 - or, 1/10th of 100, which is 10, and that's probably as small as you would want it to get.
While designing your view in Interface Builder, you will see the font size change as you change the size of the view.

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.

Content hugging with auto shrink

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 -

UILabel AutoResize cuts off the top part of the text

I have a UILabel which autoresizes along with its parent view. The label has AdjustsFontSizeToWidth turned on and has a minimum text size of 0 - so basically it tries to fit all the text into whatever size the UILabel is.
The problem I am having is that vertically the text gets cut off. So yes, the label is adjusting its font size to the width of the label but the text is too tall for the label and thus some of the text is getting cut off.
Is there anyway to work around this so that all of the text, the full height and full width are shown?
I attach an image to show what I mean. The red box is the parent view, the purple box is the UILabel.
Thanks for your help.
What you are adjusting automatically is the Width and not the Height. The Height is something you'll have to adjust manually based on the maximum font size you will use. If the maximum (assigned initial) font size fits in height, so will the smaller one's do, after they are automatically adjusted
I suspect that Lefteris is right, that minimum text size focuses on font size for the width of the control. Note, though, if you want it to resize the font to fit, you want a non-zero minFontSize. See minimizeFontSize notes. Also check out the various NSString UIKit Additions that can be used to get the size of the control necessary to fit your text, and programmatically adjust the size (i.e. the frame) your UILabel accordingly.
In my case there was a bogus vertical centering of a view under the labels being clipped and squashed. That somehow took priority over compression resistance priority of 1000 for the labels. No warning on console about conflict though. But the view debugger was of some help.

Resources