UILabel is not auto-shrinking text to fit label size

157,281

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;

Source.

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

enter image description here

  • 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

Share:
157,281
Lukas
Author by

Lukas

Updated on July 13, 2021

Comments

  • Lukas
    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
    my UIViewController receives an event and i change UILabels size. from bigger to smaller. The size of my UILabel gets smaller and i get the correct needed size, but the text in my UILabel stays the same, the same font size and etc. I need the font to get smaller, for the whole text to fit the UILabel. So the question is how to make the text to fit my label with autoshrinking 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 to wordwrap. 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
    Lukas about 12 years
    hey 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
    Lukas about 12 years
    thanks, 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
    Lukas about 12 years
    so if set number of lines to my label, the text would autoshrink then?
  • Birju
    Birju about 12 years
    if your content is out of width then it will take nextline
  • Lukas
    Lukas about 12 years
    hey i've done like you suggested, but it doesn't work, why the number of lines would help me?
  • Birju
    Birju about 12 years
    you are right i tried its not useful but i found solution for you do like that my next answer
  • lester
    lester over 11 years
    As a sidenote, minimumFontSize is deprecated in iOS 6.0. Use minimumScaleFactor instead.
  • Lukas
    Lukas over 11 years
    Hey 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
    lester over 11 years
    Yes 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
    lester over 11 years
    I 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
    Lukas over 11 years
    well 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
    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
    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
    Matthew Knippen over 9 years
    adjustsLetterSpacingToFitWidth is deprecated in iOS7
  • NicJ
    NicJ about 9 years
    Only when I changed from "Word Wrap" to "Clip" did Autoshrink work for me.
  • DesignatedNerd
    DesignatedNerd over 8 years
    When 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
    Korey Hinton over 8 years
    These 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
    Mashhadi over 7 years
    Make sure your label have all the constraints especially leading and trailing.
  • Adrian
    Adrian over 7 years
    This did the trick for me. I had just selected a UILabel and a UITextField 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
    Developer over 7 years
    lbl.adjustsFontSizeToFitWidth=YES; Worked for me!
  • Ganesh
    Ganesh about 7 years
    I have tried with the same options in Xcode8 and found not working. Please help me if anyone has the idea on this.
  • Duncan Babbage
    Duncan Babbage about 6 years
    As 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
    Chris Paveglio over 5 years
    Nov 2018: Appears the FlickType and AccessibilityKit has gone private or is removed.