UILabel AutoResize cuts off the top part of the text - ios

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.

Related

Autoshrinking text size for multiple ui labels

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.

iOS UILabel bounding rectangle not sized correctly

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.

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.

How to resize label proportional to its superview

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
//...
}

Multi-line UILabel with auto layout, how to adjust font size according to content without changing label's frame?

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.

Resources