14

I have a label that is set to adjustsFontSizeToFitWidth = YES and I need to get the actual displayed font size.

Now iOS 7 deprecated all methods that worked previously and all questions on SO suggest using these deprecated methods.

I will make this question a bounty as soon as I am allowed to by SO. Please do not close.

Vinodh
  • 5,262
  • 4
  • 38
  • 68
openfrog
  • 40,201
  • 65
  • 225
  • 373

4 Answers4

0

UILabel displayed fontSize in case of using adjustsFontSizeToFitWidth in iOS 7

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
label.text = @" Your Text goes here into this label";
label.adjustsFontSizeToFitWidth = YES;

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
// Get the theoretical font-size
[attrStr setAttributes:@{NSFontAttributeName:label.font} range:NSMakeRange(0, attrStr.length)];

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

NSLog(@"theoreticalFontSize: %f",theoreticalFontSize);
NSLog(@"AttributedString Width: %f", [attrStr size].width);
double scaleFactor=label.frame.size.width/([attrStr size].width);
double displayedFontSize=theoreticalFontSize*scaleFactor;
NSLog(@"Actual displayed Font Size: %f",displayedFontSize);

// Verification of Result
double verification=(displayedFontSize * [attrStr length]);
NSLog(@"Should be equal to %0.5f: %0.5f ", [attrStr size].width/17.0, label.frame.size.width/displayedFontSize);
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
-1

Try inserting [lblObj sizeToFit] just before requesting the font size

dpigera
  • 3,339
  • 5
  • 39
  • 60
-2

There's a readonly property that lets you do that. You can access it like this

nameLabel.adjustsFontSizeToFitWidth = YES;

//Make sure to use the line below AFTER the line above

float fontSize = nameLabel.font.xHeight;

This will give you the font size after it has been adjusted to fit width.

Amit
  • 836
  • 8
  • 19
  • -1 This answer is incorrect. Have a look at this blog: http://www.cocoanetics.com/2010/02/understanding-uifont/ —xHeight and all the other metrics of UIFont still only reflect the original font size. – daveMac Apr 29 '14 at 14:45
-2

You can get the font size of UILabel text using these line of code.

UILabel *lblObj = [[UILabel alloc]init];
lblObj.text = @" Your Text";
lblObj.adjustsFontSizeToFitWidth = YES;
float size = lblObj.font.pointSize; //Here You will get the actual size of the text.
float lineHeight = lblObj.font.lineHeight;

Try this one.

Rajesh
  • 10,318
  • 16
  • 44
  • 64
Gautam Sareriya
  • 1,833
  • 19
  • 30
  • 5
    On iOS 8, `float size = lblObj.font.pointSize` gives me the requested font size, not the actual displayed font size. – Ian Howson Apr 14 '15 at 04:36