UILabels and textFields can auto scale their fonts to fit the space of the view (as text accumulates for instance). Is there a way to measure the amount of the scale performed? As it seems, when auto scaling, the value of myLabel.font.pointsize or myTextField.font.pointSize remains the same regardless of the displayed scale of the text.
Asked
Active
Viewed 1,822 times
3
-
`UIFont` objects are immutable, you'd need to get the underlying `CTFontRef` object to get that kind of informations from the font object. – JustSid Aug 06 '12 at 15:09
-
Interesting question; why do you want to know this? I don't see any direct way of getting that information, other than estimating it based on the size of the text without scaling and the size of the UILabel. – Jesse Rusak Aug 06 '12 at 15:15
-
The reason is so I can move the label based on the scale of the font to maintain the visual spacing between text elements. – OWolf Aug 06 '12 at 16:16
1 Answers
6
There is a method to do this in the UIKit additions for NSString
:
- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
So, if you are using a UILabel, you can use the following code:
CGFloat actualFontSize;
UILabel *label = [self label];
CGSize size = [[label text] sizeWithFont:[label font]
minFontSize:[label minimumFontSize]
actualFontSize:&actualFontSize
forWidth:[label bounds].size.width
lineBreakMode:[label lineBreakMode]];
At that point, size
will contain the size of the drawn text and actualFontSize
will be the actual size of the font that the label is using for drawing.

Sebastian Celis
- 12,185
- 6
- 36
- 44
-
Thanks. The variation on this page seemed to do the trick: http://stackoverflow.com/questions/3669844/how-to-get-uilabel-uitextview-auto-adjusted-font-size – OWolf Aug 06 '12 at 16:14