UILabel is not auto-shrinking text to fit label size
Solution 1
In case you are still searching for a better solution, I think this is what you want:
A Boolean value indicating whether the font size should be reduced in order to fit the title string into the label’s bounding rectangle (this property is effective only when the numberOfLines
property is set to 1).
When setting this property, minimumScaleFactor
MUST be set too (a good default is 0.5).
Swift
var adjustsFontSizeToFitWidth: Bool { get set }
Objective-C
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
A Boolean value indicating whether spacing between letters should be adjusted to fit the string within the label’s bounds rectangle.
Swift
var allowsDefaultTighteningForTruncation: Bool { get set }
Objective-C
@property(nonatomic) BOOL allowsDefaultTighteningForTruncation;
Solution 2
This is how I get UILabel Autoshrink
work (especially for height of label font fitting in 4s device from 6s Plus Storyboard) in iOS 9.2, Xcode 7.2 ...
- Number of lines is 0
- Line Breaks: Clip
- Autoshrink: Minimum Font Scale 0.25
Solution 3
also my solution is the boolean label.adjustsFontSizeToFitWidth = YES; BUT. You must in the interface Builder the Word Wrapping switch to "CLIP". Then autoshrink the Labels. This is very important.
Solution 4
In Swift 3 (Programmatically) I had to do this:
let lbl = UILabel()
lbl.numberOfLines = 0
lbl.lineBreakMode = .byClipping
lbl.adjustsFontSizeToFitWidth = true
lbl.minimumScaleFactor = 0.5
lbl.font = UIFont.systemFont(ofSize: 15)
Solution 5
You can write like
UILabel *reviews = [[UILabel alloc]initWithFrame:CGRectMake(14, 13,270,30)];//Set frame
reviews.numberOfLines=0;
reviews.textAlignment = UITextAlignmentLeft;
reviews.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:12];
reviews.textColor=[UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.8];
reviews.backgroundColor=[UIColor clearColor];
You can calculate number of lines like that
CGSize maxlblSize = CGSizeMake(270,9999);
CGSize totalSize = [reviews.text sizeWithFont:reviews.font
constrainedToSize:maxlblSize lineBreakMode:reviews.lineBreakMode];
CGRect newFrame =reviews.frame;
newFrame.size.height = totalSize.height;
reviews.frame = newFrame;
CGFloat reviewlblheight = totalSize.height;
int lines=reviewlblheight/12;//12 is the font size of label
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(140,220 , 100, 25);//set frame as your requirement
lbl.font=[UIFont fontWithName:@"Arial" size:20];
[lbl setAutoresizingMask:UIViewContentModeScaleAspectFill];
[lbl setLineBreakMode:UILineBreakModeClip];
lbl.adjustsFontSizeToFitWidth=YES;//This is main for shrinking font
lbl.text=@"HelloHelloHello";
Hope this will help you :-) waiting for your reply
Lukas
Updated on July 13, 2021Comments
-
Lukas almost 3 years
I have this strange issue, and im dealing with it for more than 8 hours now.. Depending on situation i have to calculate
UILabels
size dynamically,
e.g
myUIViewController
receives an event and i changeUILabels
size. from bigger to smaller. The size of myUILabel
gets smaller and i get the correct needed size, but the text in myUILabel
stays the same, the same font size and etc. I need the font to get smaller, for the whole text to fit theUILabel
. So the question is how to make the text to fit my label withautoshrinking
or something like that?In my
xib
,UILabels
autoshrink
is checked, also number of lines is set to 0, and also my string has new line symbols (\n), and i've selected linebreakmode towordwrap
. Maybe anyone was in the same situation as i am now, and could help me? I would really appreciate that.Thank's in advance!
EDIT:
UILabel
minimum font size is set to 10 -
Lukas about 12 yearshey thanks for your answer, but i don't need to calculate the height of label, i have fixed size, and depending on that my font has to get smaller if needed.
-
Lukas about 12 yearsthanks, but this won't calculate or shrink font to minimum needed as i understand, and also why is your minimum font larger then normal?
-
Lukas about 12 yearsso if set number of lines to my label, the text would autoshrink then?
-
Birju about 12 yearsif your content is out of width then it will take nextline
-
Lukas about 12 yearshey i've done like you suggested, but it doesn't work, why the number of lines would help me?
-
Birju about 12 yearsyou are right i tried its not useful but i found solution for you do like that my next answer
-
lester over 11 yearsAs a sidenote,
minimumFontSize
is deprecated in iOS 6.0. UseminimumScaleFactor
instead. -
Lukas over 11 yearsHey thanks for your help, but i've tried that and it didn't help. It seems that auto shrink doesn't work for multiple lines. But i don't understand why it is like that..
-
lester over 11 yearsYes auto shrink doesn't work for multiple lines in UILabel. Try to imagine this: you have text with multiple lines, you want to shrink it so that it "fit" with the width. So should it shrink the whole text to fit in one line, or words stay in the same line and shrink to fit each width? The latter is the most common case, but do not forget that the words is also set to arrange themselves in multiple lines. Even if auto-arrangement of the text is disabled, you will have to face with each lines of the text having different font size, as not every words will fit to the width.
-
lester over 11 yearsI think this answer with a Category here is quite good, it might even be what you have implemented anyways. This is definitely something missing in Apple API, or maybe they just don't see shrinking should be a common use case for multiple lines of static text.
-
Lukas over 11 yearswell yes that's a good point, but anyway, i think if i set the number of lines to 0, thats kind of a clear statement that there could be one or more lines. And autoshrink could be deterimend by the line numbers i guess.. But anyway, thanks for clearing things out. I thought i was doings something wrong. A categorie is a good idea, i guess i'm going to do that in other project where i will need this. Appreciate your help, good luck ;)
-
devios1 over 10 years@lester It should shrink so that the entire text is visible in the number of lines declared for the label given its current dimensions. It doesn't need to attempt to make each line a different size. I'm really surprised it can't figure this out. So if I have a (potentially) long string to display I can either have multiple lines or have it auto-size, not both. That's lovely.
-
Adam about 10 years@lester this is pretty easy. Apple's own CoreText library (that drives UIKit) has the feature built-in. The obvious reason it's missing / broken in IB/iOS for multiline labels is that Apple forgot to fix it when they added CoreText to iOS. Their bugtracking is notoriously poor.
-
Matthew Knippen over 9 yearsadjustsLetterSpacingToFitWidth is deprecated in iOS7
-
NicJ about 9 yearsOnly when I changed from "Word Wrap" to "Clip" did Autoshrink work for me.
-
DesignatedNerd over 8 yearsWhen I changed word wrap to clip, I was actually able to get it to resize in a label with zero lines. Boy it'd be nice if this was documented.
-
Korey Hinton over 8 yearsThese are the 3 ingredients needed for a label to actually shrink, I was missing the clip line break setting. Also, if its adjacent to other labels (that also might auto shrink) then compression/hugging priorities need to be set or explicit width or height constraints given. This should be the currently accepted answer.
-
Mashhadi over 7 yearsMake sure your label have all the constraints especially leading and trailing.
-
Adrian over 7 yearsThis did the trick for me. I had just selected a
UILabel
and aUITextField
in a static cell and clicked "Resolve AutoLayout Issues", and "Add Missing Constraints". Everything lined up, but Autoshrink needed to know the distance to the label, which "Add Missing Constraints" did not add a constraint for. -
Developer over 7 yearslbl.adjustsFontSizeToFitWidth=YES; Worked for me!
-
Ganesh about 7 yearsI have tried with the same options in Xcode8 and found not working. Please help me if anyone has the idea on this.
-
Duncan Babbage about 6 yearsAs per other answers here, the key thing missing was you need to set Word Wrapping to "Clip". It's counter-intuitive, but this is required to get a UILabel to auto-shrink in the way you want. This works with a multi-line UILabel.
-
Chris Paveglio over 5 yearsNov 2018: Appears the FlickType and AccessibilityKit has gone private or is removed.