Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UILabel font pointsize after minimumFontSize

I have a UILabel with a font of point size 17. If I call label.font.pointSize I get 17, which is all good. BBUUUUTTT I also have a minimumfontsize set to 8, now if I cram some text in the label which causes the point size to shrink and then call label.font.pointsize I still get 17 even though I know the point size is smaller

Any ideas how to get the true point size after system has resized the font?

like image 426
MobileMon Avatar asked Jun 04 '13 15:06

MobileMon


1 Answers

I don't know of an API to get the current point size of the UILabel when it is scaling your content down. You can try to approximate "scaling factor" using sizeWithFont APIs.

Just an idea:

// Get the size of the text with no scaling (one line)
CGSize sizeOneLine = [label.text sizeWithFont:label.font];

// Get the size of the text enforcing the scaling based on label width
CGSize sizeOneLineConstrained = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size];

// Approximate scaling factor
CGFloat approxScaleFactor = sizeOneLineConstrained.width / sizeOneLine.width;

// Approximate new point size
CGFloat approxScaledPointSize = approxScaleFactor * label.font.pointSize;
like image 156
Sonny Saluja Avatar answered Oct 19 '22 08:10

Sonny Saluja