2

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:

Minimistro
  • 73
  • 7

1 Answers1

0

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

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31875485) – Helge Becker Jun 02 '22 at 09:59