I have a multiline UILabel that I'd like to look similar on different devices (iPhone only). If it was a single line, I'd simply place the auto layout constraints and enable Autoshrink and set the Minimum Font Scale.
The height of the label is dynamic is calculated based on text. To do that, I need the font, which should also be dynamic. The hack I could do is place an invisible label, and set the text I want to fit in a single line into it and calculate the font, but it seems too hacky.
Another thing I'm not that familiar with are size classes. But from what I've read, the same class is shared between all portraits, meaning 3.5, 4, 4.7 and 5.5 inch devices would be bound to the same class, therefore I couldn't use the separate font value?
How would I implement the 'font scaling' for multiline labels so I'm getting similar look on different screen sizes?
If by similar look you mean that the same words appear on each line, I don't think it's possible.
If the label height is dynamic based on the amount of text, the label will simply expand to the height required to show the text with the specified font size.
Font scaling only kicks in when there is not enough space to display the text with the specified font size. Therefore, you must constrain the height or number of lines.
I was able to approximate this using a multiline UILabel with font scale of .5. I set an Equal Heights constraint between it and its superview, with a multiplier of .25. See screenshots below.
In this approach, you would have to dynamically change the Equal Heights multiplier based on the amount of text you have.
Related
I have five ui labels for one view. All of them have variable text content. To prevent truncating I have to use Autoshrink text size.
My ui labels are aligned in x axis top to bottom.
The problem is; when autoshrinking, all ui labels have different font sizes.
Is there a way to equalize font sizes of all ui labels when autoshrink.
I use interface builder autolayout constraints.
Thank you.
You could loop through your labels, find the smallest text size and then set that text size to all of them. That way, you know they'll all fit and have the same text size. You would have to do this programmatically. Alternatively, you could dynamically change the size of your UILabels based on the number of characters it holds times some constant factor. That way, the autoshrink should set the same size for each the text in each of the labels.
I want to fit my text in a UILabel, but for different iPhone the size of the UILabel is changing, as I'm using auto layout, but I cannot fix the font size, so my text is cutting out.
Is there any way I can set any constraint so that the text fits in the UILabel dynamically?
See here the text got cut, because of different screen resolution
You should use autoshrink.
Since all iPhones have the same Compact width size class when in portrait mode, you can't rely on this to handle your label size.
Previews are for iPhone5, iPhone6 and iPhone 6+
In the inspector, you must select minimum font scale or minimum font size in front of Autoshrink. This enables the content to change the size of the font to fit in the label.
Here, I set minimum font scale to 0,5 so the minimum size is half of the current size (31.0). The text will try to fit until it reaches the minimum scale/size.
(Generally do not use "Tighten letter spacing" for this purpose. Tighten letter spacing uses the same font size and reduces spacing between letters. It can make the label up to 5% tighter before truncating, but it's not effective when minimum font scale/size is enabled.)
You may want to test with a wide screen device such as the iPad Pro, and also on a smaller screen such as the iPhone 4S. As a good practice you should test with different user system font sizes, you can set them in Settings>General>Accessibility>Larger Text.
Autoshrink will not adjust the font size bigger than the one set on the label, that means if you make the label the same width as the screen but leave the font size to 14, it will try to increase the font size until it reaches that size.
To make it actually work, select a big font size.
You can still combine autoshrink with size classes to change the maximum font size depending on the device/the orientation.
In case you want to use autoshrink with UIButtons, you can still set this behavior with two lines of code.
myButton.titleLabel.minimumScaleFactor = 0.5;
myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
Hi if you are adding UILabel from storyboard you can set different font for all available layout.
You can do this by using size classes.
each display dimension using a size class, this will result in four abstract devices: Regular width-Regular Height, Regular width-Compact Height, Compact width-Regular Height and Compact width-Compact Height.
The table below shows the iOS devices and their corresponding size classes.

To set the font size for this particular size class, first select the UILabel. Under the Attributes inspector, you should see a plus (+) button next to the font field. Click the + button and select Compact Width > Regular Height (Or as per your requirement select width & Height). You will then see a new entry for the Font option, which is dedicated to that particular size class. Keep the size intact for the original Font option but change the size of wC hR (OR as per your selection) font field to required points (for example 14).
I have a UILabel (as highlighted in yellow), which has the following conditions applied to it.
Label has variable text length
Font set to 40
Minimum font-size set to 20
Number of lines set to 3
Although this looks like a duplicated question I believe it is not. The issue I am having is that when the text exceeds the available 3 line length after being sized-down to 20 points, the UILabel's bounding box is sized incorrectly (i.e. note the extraneous spacing above and below the text).
The end result should be a UILabel without any spacing. Is there are solution to this, while keeping the number of lines set to 3?
That looks like a bug. If you increase your base font size, you will see the space increase. Also, if you inspect the layout at runtime, you will see the content size to be calculated as too big.
My guess is, UILabel takes your original font size (40) to calculate the content size for 3 lines of text and does not take into account that the font size has already been decreased before truncation.
I fiddled with content hugging/compression priorities but could not make it work either.
The only workaround I found was to manually set the font size down to 20. That will get you the frame you want.
I have multiple labels in the storyboard using the any X any Resolution as follows:
Now I have added constraints to the best of my knowledge to force the labels to reposition when an iphone simulator is used and the following happens.
What I would like to happen is have the labels remain the same distance apart from each other proportionally and just resize their font size. So If we were to divide the Iphone screen into thirds Days would occupy the first 3rd, months the 2nd, and Years the 3rd. I thought maybe using aspect ratio for constraints would work but I got the same effect of the labels bunching on top of each other. It looks like they maintain the constraint of distance from the edges but they wont resize themselves to keep the ratio.
Thanks
If you want to keep same distance between each of them, one way of achieving it is:
Give all of them equal widths constraint. and set leading and trailing constraint to zero. Align all labels to centre horizontally.
This way, you wont need to change the font size..
But, however, if you still want to change font, go to attributed inspector for label and under auto shrink: change fixed font size to minimum font size and set it to appropriate value, say 14.
I have a multi-line UILabel, which will display contents at run time, contents may only need 1 line or up to 3 lines.
When there is less contents, I'd like the label using a larger font size, and when there are more contents, I want the label to use multiple lines and smaller font size.
Due to autolayout, I can't set the frame of UILabel, and the frame of the label is unknown until run time, so that it can be compatible with different devices.
Currently I set line number of the label as a fixed number, which will divide contents into multiple lines when there is more contents, but when there is less contents, the single line uses a font size as small as when there is more contents. This is not the best experience.
set num of lines and font scale factor 0.5 or 0.75
If your content is small it would use the occupied lines and font size will be the actual size. When your content grows these properties will check if with actual fontsize the content can be adjusted to all lines or not. if its not adjustable with full size then font will be scalled to adjust in contentview.