31

When myLabel.adjustsFontSizeToFitWidth = YES, UILabel will adjust the font size automatically in case the text is too long for the label. For example, if my label is just 100px wide, and my text is too long to fit with the current font size, it will shrink down the font size until the text fits into the label.

I need to get the actual displayed font size from UILabel when the font size got shrunk down. For example, let's say my font size was actually 20, but UILabel had to shrink it down to 10. When I ask UILabel for the font and the font size, I get my old font size (20), but not the one that's displayed (10).

Pang
  • 9,564
  • 146
  • 81
  • 122
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
  • 3
    None of the answers under this question actually answer the question being asked. Not sure why that is the case, but it is. Here is an actual answer for future readers: https://stackoverflow.com/a/28285447/2057171 – Albert Renshaw Sep 20 '17 at 02:44

5 Answers5

6

I'm not sure if this is entirely accurate, but it should be pretty close, hopefully. It may not take truncated strings into account, or the height of the label, but that's something you might be able to do manually.

The method

- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

will return the text size, and notice that it also has a reference parameter for the actual font size used.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ed Marty
  • 39,590
  • 19
  • 103
  • 156
  • 4
    This method is now deprecated under iOS 7. See http://stackoverflow.com/questions/19195541/how-to-calculate-actual-font-size-in-ios-7 – openfrog Oct 05 '13 at 08:15
  • 2
    That will also return the bounding box size for the font size given... Not the font size for the bounding box size given. – jowie Jul 17 '14 at 14:12
  • @jowie it doesn't return the font size, but it does give it to you - for `actualFontsize:` you dereference a pointer and it puts the value there - `actualFontSize:&mySizeCgFloat` like this – Alexandre G Dec 14 '15 at 06:41
4

In case anybody still needs the answer. In iOS9 you can use boundingRectWithSize:options:context: to calculate actual font size. Note that context.minimumScaleFactor should not be 0.0 for scaling to work.

- (CGFloat)adjustedFontSizeForLabel:(UILabel *)label {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
    [text setAttributes:@{NSFontAttributeName:label.font} range:NSMakeRange(0, text.length)];

    NSStringDrawingContext *context = [NSStringDrawingContext new];
    context.minimumScaleFactor = label.minimumScaleFactor;
    [text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:context];
    CGFloat adjustedFontSize = label.font.pointSize * context.actualScaleFactor;

    return adjustedFontSize;
}
marchozaur
  • 135
  • 1
  • 3
2

For one-line UILabel works fine this simple solution:

//myLabel - initial label

UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = myLabel.font;
fullSizeLabel.text = myLabel.text;
[fullSizeLabel sizeToFit];

CGFloat actualFontSize = myLabel.font.pointSize * (myLabel.bounds.size.width / fullSizeLabel.bounds.size.width);

//correct, if new font size bigger than initial
actualFontSize = actualFontSize < myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;
Igor
  • 12,165
  • 4
  • 57
  • 73
1

Swift 5

For one-line UILabel

extension UILabel {

    var actualFontSize: CGFloat {
    //initial label
     let fullSizeLabel = UILabel()
     fullSizeLabel.font = self.font
     fullSizeLabel.text = self.text
     fullSizeLabel.sizeToFit()

     var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);

    //correct, if new font size bigger than initial
     actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;

     return actualFontSize
    }

}

Getting the actual font size is then as simple as:

let currentLabelFontSize = myLabel.actualFontSize
Merichle
  • 693
  • 11
  • 15
  • This seems to be off by a little for the font size it's calculating which is important in my case, any ideas why this might be? – Joseph Astrahan May 08 '20 at 06:00
0
UILabel *txtLabel = [[UILabel alloc] initWithFrame:rectMax];
txtLabel.numberOfLines = 1;
txtLabel.font = self.fontMax;
txtLabel.adjustsFontSizeToFitWidth = YES;
txtLabel.minimumScaleFactor = 0.1;
[txtLabel setText:strMax];

UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = txtLabel.font;
fullSizeLabel.text = txtLabel.text;
fullSizeLabel.numberOfLines = 1;
[fullSizeLabel sizeToFit];

CGFloat actualFontSize = txtLabel.font.pointSize * (txtLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
actualFontSize = actualFontSize < txtLabel.font.pointSize ? actualFontSize : txtLabel.font.pointSize;
// the actual font
self.fontMax = [UIFont fontWithName:self.fontMax.fontName size:actualFontSize];

my code works great, part from @Igor

hqlulu
  • 1