adjustsFontSizeToFitWidth not working with iOS 15 UIButton

Viewed 416

I want to size the text on my button to automatically fit to the edges of the button.

This code was working pre-iOS15, now it doesn't.

testButton.titleLabel?.adjustsFontSizeToFitWidth = true
testButton.titleLabel?.minimumScaleFactor = 0.1

How can I automatically resize the text to fit the size of my button in iOS 15?


This is playground code for testing. It includes everything I've tried, but still isn't working.

import UIKit
import PlaygroundSupport

//building button
let testButton = UIButton(frame: CGRect(x: 100, y: 100, width: 300, height: 300))
testButton.setTitle("test", for: .normal)

//everything I've found in other answers that is supposed to work
var titleLabel = testButton.titleLabel!
titleLabel.font = UIFont.systemFont(ofSize: 300)
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.1
titleLabel.numberOfLines = 1 //also tried 0 instead
titleLabel.lineBreakMode = .byClipping

//configuring button
testButton.configuration = UIButton.Configuration.filled()

//display the button
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
containerView.backgroundColor = .white
PlaygroundPage.current.liveView = containerView
containerView.addSubview(testButton)

Related questions I've already looked at and tried:

1 Answers

I found out it could be caused by testButton.configuration = UIButton.Configuration.filled(), but I don't know why.

Related