简体   繁体   中英

Adjust font size to fit label height - Swift 4, Xcode 9

How do we increase the font size of label to match the height of the label?

In Swift 4 or Storyboard for Xcode 9, either programmatically or via interface builder.

I have attempted the following, which has been claimed to work by a few in Swift 3. - SO Q&A

label.minimumScaleFactor = 0.1
label.adjustsFontSizeToFitWidth = true
label.lineBreakMode = .byClipping
label.numberOfLines = 0

For testing purposes, I created a label that is the width of the screen and the height of the screen, yet the font size remains the default size. I also made the settings in the interface builder match that of the code above, with equally unsuccessful results.

Have a simply solution for this. We can increase size default of label to 100 and set property min font size of the label ~ 1. I dont remember exactly this property. Im on mobile

在情节提要板上设置标签,然后将字体大小设置为最大值,然后在属性检查器中设置标签的属性最小字体大小,然后根据screwn大小进行调整。

Answering my own question. Answer found buried in the post I linked above.

If your UILabel has a fixed height, whether it be by points or by a multiplier of a superview, the font size can be set to any fraction of that height with one line of code:

testLabel.font = testLabel.font.withSize(testLabel.frame.height * 2/3)

where 2/3 is the height fraction of the label the text occupies.

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