简体   繁体   中英

Best way to adjust font size with width and height of UILabel

I have a UILabel that grows according to the screen size (Using auto resizing). However, the font isn't upscaling. I tried setting the label's adjustFontSizeToFitWidth property to YES, but it isn't working. Here is my code.

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        outLabel.numberOfLines = 1
        outLabel.adjustsFontSizeToFitWidth = true
        outLabel.textAlignment = NSTextAlignment.center  
    }

I am not sure what to do at all. The font size stays the same no matter what.

I implemented something similar to this and the way I solved the problem is by setting the font size at a high number to begin with. adjustsFontSizeToFitWidth should work to downscale your font size according to the width.

For example, set font size at 20 and adjustsFontSizeToFitWidth will downscale the font size to fit current UILabel width.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM