Dynamicaly scale the fonts in UIButton

Viewed 609

Just renovating my previous question How to scale the fonts in a Button still have no answer The problem description - I have some UIButtons created from the main.storyboard interface. The buttons have some tittles. As I use the constrains the button size increases as I change the device screen size. At the same time the tittles font remains the same so I have large buttons with very small tittles. When I use the devices with the small screen as iPhone4 the tittles go out of the button edges. I tried

@IBAction func touchDigit(_ sender: UIButton) {
        sender.titleLabel?.minimumScaleFactor = 0.5;
        sender.titleLabel?.numberOfLines = 0
        sender.titleLabel?.adjustsFontSizeToFitWidth = true

I made the class for the buttons with the inspectable var

@IBInspectable var adjustFontSize : Bool {
set { titleLabel?.adjustsFontForContentSizeCategory = newValue }
get { return titleLabel!.adjustsFontForContentSizeCategory  }
   }

that have no effect. UILabel have Automatically Adjust Fonts checkbox. UIButton have not. I'm sure that everyone had such a problem and the solution seems to be simple. Please help to find it!

1 Answers

Use this :

sender.titleLabel?.lineBreakMode = .byClipping
Related