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

前端 未结 8 1220
一整个雨季
一整个雨季 2020-12-01 15:13

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.<

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 15:31

    Simple solution for one-line UILabel:

    //myLabel - initial label
    
    UILabel *fullSizeLabel = [UILabel new];
    fullSizeLabel.font = myLabel.font;
    fullSizeLabel.text = myLabel.text;
    [fullSizeLabel sizeToFit];
    
    CGFloat actualFontSize = myLabel.font.pointSize * (myLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
    
    //correct, if new font size bigger than initial
    actualFontSize = actualFontSize < myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;
    

提交回复
热议问题