Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate actual font point size in iOS 7 (not the bounding rectangle)?

Edit: The linked "duplicate" question only deals with calculating text rectangle. I need to calculate actual font size after label scaled it, NOT the string size.

This method is now deprecated:

size = [self sizeWithFont:font // 20
              minFontSize:minFontSize // 14
           actualFontSize:&actualFontSize // 16
                 forWidth:maxWidth
            lineBreakMode:self.lineBreakMode];

How can I calculate font size of a UILabel now in iOS 7 when it shrunk the size of the text to fit in?

like image 701
openfrog Avatar asked Oct 05 '13 08:10

openfrog


1 Answers

I have the same problem, I need to know the actual size to make that the others UILabels in my UIView match.

I know that it's not a perfect solution, but perhaps it's useful for you.

My solution is: instead of use adjustsFontSizeToFitWidth I calculate "manually" the size.

CGSize initialSize = [_label.text sizeWithAttributes:@{NSFontAttributeName:_label.font}];
while ( initialSize.width > _label.frame.size.width ) {
    [_label setFont:[_label.font fontWithSize:_label.font.pointSize - 1]];
    initialSize = [_label.text sizeWithAttributes:@{NSFontAttributeName:_label.font}];
}
CGFloat actualSize = _label.font.pointSize;
like image 80
Ferran Martin Avatar answered Sep 16 '22 19:09

Ferran Martin