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.
Related
I am creating a launch screen that has a centered image. I want the image to be 0.7 of the width in portrait orientation and 0.7 of the height in landscape orientation, so the image is the same size in both orientations, but relative to device size. It would be smaller on iPhones and larger on iPads, while supporting both orientations on iPad.
I can do one of those constraints but not both. For example, if I make it 0.7 of the width in portrait then it looks good in portrait but then oversized in landscape, and vice versa.
How do I create such autolayout purely in interface builder? (I'm assuming launch screens must only use IB.)
The trick is to add two sets of proportional width and height constraints...
First:
centerX
centerY
1:1 aspect ratio
Then:
width <= 0.7 #1000
width = 0.7 #750
height <= 0.7 #1000
height = 0.7 #750
So our constraints are saying:
try to make width and height each at 70% -- Priority: 750 (High, but not Required)
but keep the image view at 1:1 ratio -- Priority: 1000 (Required)
and neither width nor height can be greater than 70% -- Priority: 1000 (Required)
Here's how it looks in the Document Outline pane:
and here's what we get for iPad 9.7":
and iPhone 11:
You need to make constraints in respective of the view. For that, you need to follow the steps mentioned below :
Select your imageView.
Hold the control key and move your cursor to the main view.
Now click Equal widths constraint and give it the desired multiplier.
Repeat the above steps for the Equal Height constraint.
You are ready to go.
To make this work requires adding the equal widths constraints (per #Saumya Gautam's answer - welcome to SO!) but requires a little more tweaking to give the results.
Create equal width constraints to desired percentage (image to super view).
Update constraints: set one of them to "less than or equal" relation and set the other at a priority less than the first, e.g. 900.
Create aspect ratio (1:1) constraint on image.
Constraints:
Result, in portrait:
In landscape:
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:
A view at the bottom of a view controller with the following constraint:
leading - 0
trailing - 0
bottom - 0
aspect ratio - 7:1
it looks good on small screen sizes, but on larger screen the view looks too small. What is the best way to increase the aspect ratio according to what screen is using the application
Instead of having aspect ratio set, use proportional height and proportional width. To do so:
1- Select your view and set its height equal to the height of its superview
2- Click Edit and then change "proportional" to whatever you want:
Now you have a view that will be proportional to its superview height. Do the same for its width if you need it.
To be more precise, you can play with the priority of the constraint. For instance keep your ratio constraint and set its priority to 999.
Then change both proportional constraints (those we just add) constant to ">=" instead of "=". Then try different values of multiplier to see how the view react. By doing this you might need a maximum height/width constraint as well (as the view won't know in certain cases which size to choose).
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 -
Hi I am trying to get 4 rectangles to be each 1/4 of the screen size for each IPhone size from 4 to 6.
I have tried adding constraints and setting the rectangles to equal height but still haven't succeeded.
Is there an easy way to set to set this?
Thank you
You have to add the equal heights constraint to every rectangle (from the rectangle to the view, which has the exact height of the screen), and then click on the constraint, and set the multiplier to 1:4.