By design (!) I have two lines title text which I cannot break:
JustWord
VeryLongWordWithoutSpaces
On big iPhones (6 and 6 Plus) text works great,but when I test on smaller iPhone (5 or less) it looks like:
JustWord
VeryLongWordWi...
I cannot find a way how to shrink the text in UILabel.Cutting and truncating are not possible in my situation.
Requirements:Auto Layout, no Storyboards, Swift 2.0, iOS 8.0+
Please try adjustsFontSizeToFitWidth: property for UIlabel. I have tried this and font is setting in one line and font size is automatically adjust.
The two principal ways to do this are to set a minimum font size or a minimum font scale.
Related
I have been working on a project where I need to implement a simple messaging / chatting UI. For test purposes, while using Lorem Ipsum I have noticed that, even after having a multiline label (and the label is acting as multiline) some of the texts are being cut off and shown three dots when running in smaller iPhones like SE or 5s. Like the following:
But this doesn't occur if I run it in 8plus or 7plus.
After a lot of trial and error, I found that if I set the auto-shrink to minimum font scale or minimum font size the problem is solved. Like this:
But that makes the texts unequal sized to see. I don't know why its happening.
I think you should label.numberOfLines = 0 and make Row height and Estimate to Automatic
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 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.
I have 2 buttons with the same width and height, also y-Achse centered. They differ only from text length. With iPhone 5, 6 and 6+ they look very well. But with iPhone 4s one button will do not more have the same front size as the other one and it is text will be not more centered. My question is if there is a method to adopt the font size of the 2 buttons so they will take together the same font with small value of font size of one button.
If I understand correctly, you want to use the same font size for both buttons, even when one of them has to reduce the size for the text to fit it.
Use something like this to reduce the size of the font on each button, until an appropriate size is found for both buttons. Take the minimum of the sizes you found, and use it on both buttons.
I have an issue when setting minimum font size on my UILabels. As you can see the shadow isn't following along with the font size. It's supposed to be just 1 px heigh.
My label:
My settings:
Turn off "Tighten Letter Spacing".
There seems to be a bug with shadows, auto shrinking, and auto tightening.
Set the shadow.offset programmatically based on font size.
You have set the Value 12 Below the Minimum Font Size you can't set this value its Too large pass it like 0.5 or 0.8 something Like that.
Secondly your Vertical Shadow Offset is 1 so there is no chance to take that much of shadow It may be possible that you have passed shadow through Code please check it first.
When you check it in xib File while changing at that time if its not change that just reopen the Window than It'll Work.
Hope this will Help.