kesape
kesape

Reputation: 159

Button subtitle label

One day i did button with title label, subtitle label and image in storyboard and got this effect

Image of button in storyboard

but now when i am trying to do this in code programmly i have a problem, i dont see subtitle..

private var firstPaidPack: UIButton = {
        let button = UIButton(type: .custom)
        let image = UIImage(systemName: "circle")
        button.layer.cornerRadius = 15
        button.backgroundColor = UIColor(white: 1, alpha: 0.3)
        button.setImage(image, for: .normal)
        button.setTitle("1.99$", for: .normal)
        button.subtitleLabel?.textAlignment = .center
        button.subtitleLabel?.text = "subtitle text to check how it..."
        button.titleLabel?.font = UIFont(name: "Arial", size: screenWidth / 375 * 19)
        return button
    }()

Upvotes: 1

Views: 2510

Answers (2)

Tushar Lathiya
Tushar Lathiya

Reputation: 1048

Try the below tricks will help you to change the subtitle of the button.

if #available(iOS 15.0, *) {
    button.configuration?.subtitle = "Your Subtitle"
}

Have a look here for a detailed answer.

Upvotes: 1

Farid Rahmani
Farid Rahmani

Reputation: 79

You need to create a UIButton.Configuration and set the subtitle on the configuration object:

var config = UIButton.Configuration.tinted()
config.subtitle = "subtitle text to check how it..."
// Set title and all other properties on the configuration object...
let button = UIButton(type: .custom)
button.configuration = config

You can also set the title and all other properties on the configuration object instead.

Upvotes: 7

Related Questions