Home > Enterprise >  Button text shrinks when the text is changed
Button text shrinks when the text is changed

Time:12-01

Ones I change the text in button it's getting stink to font size of ~18 even though initial font size is 55. Button is not custom. It has System font.

button.setTitle("Some button text", for: .normal)

I tried manually change font size in code but it doesn't help

button.titleLabel?.font = UIFont.systemFont(ofSize: 55)

I also tried to change the name through configuration function, but there is no font size and it shrinks anyway.

`

button.configuration = paperButtonConfiguration()

func buttonConfiguration() -> UIButton.Configuration {
        var config: UIButton.Configuration = .plain()
        config.title = "Some button text"
//Should probably be something like this but it doesn't work
//config.font = UIFont.systemFont(ofSize: 55)
        return config
    }

`

While I was writing this question I kind of solve this, but not completely. If I change the button style from "plain" to "default" it works, but some weird animation of fading and appearance occurs.

How could I do this with "plain"?

CodePudding user response:

UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on. If you want to old behaviour , you must set style plain to default.

Or , If you want one of the style above . You need to set font like

button.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
  var outgoing = incoming
  outgoing.font = UIFont.systemFont(ofSize: 50)
  return outgoing
 }

CodePudding user response:

Navigate to your storyboard and change your Button attribute to adjustsFontSizeToFitWidth

in this link

titleLabel.adjustsFontSizeToFitWidth to true

https://stackoverflow.com/a/36151101/7826869

  • Related