iOS UILabel text of different sizes dynamically loading? - ios

For example I have fixed width label with 4 word text with a fix font size. but when 4 (small )word sentence "this is my car" and other 4 (large)words sentence "elephant most beautiful animal" with same size font.
so small 4 words sentence will be adjusted in fixed label width but bigger word sentence "elephant most beautiful animal" will cause the problem in that UILabel text
How can I dynamically calculate the size of sentence words and adjust font size of text to be fixed in UIlabel.
please guide...

Probably you're looking for the adjustsFontSizeToFitWidth property.

in interface builder click on the label and set the minimum font size. if it is not in interface builder, do this
[Label setAdjustsLetterSpacingToFitWidth:YES];
[Label setMinimumScaleFactor:2];
[Label setAdjustsFontSizeToFitWidth:YES];
these shrink the font size depending on the length of the sentence. The first tightens the letter spacing and the second scales the font down by 2. The 3rd adjusts the font size.

Related

How to apply same for size for all labels in a stackview?

I have a horizontal stackview filled equally with 3 labels. These labels have set adjustsFontSizeToFitWidth and minimumScaleFactor values set. 2 labels will have short texts and one may have large text. Now the large title label's font is reduced automatically and I want the other two labels to automatically update their font size to match the last label font size. Is it possible?
Label1 text - "a"
Label2 text - "b"
Label3 text - "Something long"
Now Label3's font looks smaller than Label1 and Label2.

How to use biggest font in UILabel as possible?

In my case UILabel size can change dynamical . If it is bigger I want to have the best fitting, biggest font size. Is it possible to set it in Storyboard or I need calculate the best one programmatically?
If you have more than 1 word in the label, you can set a very high font size and, set AutoShrink -> Minimum Font Size to a small size. Then in run time app will auto shrink to maximum possible font size. But this doesn't work for if you only have a single word.

How to dynamically calculate the number of characters a UILabel holds before it gets truncated?

I have a UILabel that is configured with dynamic parameters:
textLabel.font = UIFont.systemFontOfSize(some_dynamic_size, weight: some_dynamic_font_weight)
textLabel.numberofLines = some_dynamic_number_lines
textLabel.frame = CGRectMake(0, 0, dynamic_width, dynamic_height)
How to calculate the number of characters this UILabel can hold before it gets truncated?
Calculate how tall the label would want to be given its font, text, and width. If that is taller than you want to make it, the text won't all fit.
The issue is that your label will be able to fit more or less text given on the font that you use and the size that it is set too. If you set that to a standard size say for instance San Francisco at 12pt then you will still have a varying amount of letters that can fit because not all letters are the same width for instance 'WWW' is allot bigger than ' lll ' but you could determine a "best estimate" by taking the average letter width. Then dividing the size of the label by that then you would have a general idea of how many letters could fit but you also need to account for the '...' that is inserted.
But I would suggest not concatenating at all and just make the text a variable size so it can shrink and show the whole word. I suggest looking into 'dynamic text' but I don't know what your application is so that may not be the best suggestion. Hope that helps 😜

Auto size UILabel text

I have a UILabel with multi lines. I'm trying to get the text to auto size, and fit on the line it's in. So instead of:
longe
st
The text size should become smaller so that it can fit on the same line:
longest
Note: I only need it for the first line.
I tried the following:
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor=3.0;
That didn't work, so I tried label.minimumScaleFactor=3.0 / 9; (9 is the font size.) That also didn't work.
I have a UILabel with multi lines
Well, that's the problem. The automatic size adjustment feature of UILabel works only for 1-line UILabels (i.e. numberOfLines must be 1).
You might be happier with two labels, one being a single-line UILabel for the first line which can shrink its size, the other for the remaining lines.

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