Imagine a UILabel, which is 200 pixels wide and 50 pixels high. The label has text inside, and the label makes the text smaller so that it fits into the label. But now, how would you get the size of that UIFont how it is visible in the label? Lets imagine the font size was given with huge 100, and the label squeezes it down to 15. And then, you want to make some other labels with little text, which has same font size. Is there a way to obtain the UIFont's font size after getting squeezed by the label?
Asked
Active
Viewed 9,028 times
1 Answers
14
If you pass the size of the UILabel and the breakMode, etc. to:
CGSize size = [label.text sizeWithFont:label.font minFontSize:10 actualFontSize:&actualFontSize forWidth:200 lineBreakMode:UILineBreakModeTailTruncation];
actualFontSize should be what you are looking for.
UPDATE:
The above has been deprecated. The method to use now is:
- (CGRect)textRectForBounds:(CGRect)bounds
limitedToNumberOfLines:(NSInteger)numberOfLines
Here's an example
CGSize size = [label textRectForBounds:label.bounds
limitedToNumberOfLines:1].size;

mahboudz
- 39,196
- 16
- 97
- 124
-
So actualFontSize would be a CGSize which I pass by reference to that method? – Sep 06 '09 at 13:26
-
Yes. In C and Obj C, a function is restricted in how it can return values. Arguments you pass to a function are evaluated and their value is pushed onto the stack, before the function is called. The function gets only those values and can't relay a return value in them. Unless what is pushed is a pointer, in which case the function can use pointer to write to its address and modify it. I'm oversimplifying - here actualFontSize is one of the returned values of the function (method). – mahboudz Sep 06 '09 at 18:02
-
This is deprecated in iOS7. Is there an iOS8 replacement? – Shaun Budhram Nov 13 '14 at 17:39
-
`- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines` is the closest I can thin k of right now. – mahboudz Nov 14 '14 at 09:20
-
@mahboudz your solution isn't what the question asks! – Zeb Jul 07 '15 at 13:40
-
You are right, the OP was asking for a font size and not a size that contains the string. I don't know if getting the font size is possible. – mahboudz Jul 09 '15 at 00:08