How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?

后端 未结 5 2130
醉话见心
醉话见心 2020-11-30 05:33

When myLabel.adjustsFontSizeToFitWidth = YES, UILabel will adjust the font size automatically in case the text is too long for the label. For example, if my lab

5条回答
  •  感动是毒
    2020-11-30 06:27

    I'm not sure if this is entirely accurate, but it should be pretty close, hopefully. It may not take truncated strings into account, or the height of the label, but that's something you might be able to do manually.

    The method

    - (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

    will return the text size, and notice that it also has a reference parameter for the actual font size used.

提交回复
热议问题