按钮副标题标签

kes*_*ape 1 button swift

有一天,我在故事板中制作了带有标题标签、副标题标签和图像的按钮,并得到了这种效果

故事板中按钮的图像

但现在当我尝试在代码中以编程方式执行此操作时,我遇到了问题,我看不到字幕..

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
    }()
Run Code Online (Sandbox Code Playgroud)

Far*_*ani 6

您需要创建一个对象UIButton.Configuration并对其进行subtitle设置configuration

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
Run Code Online (Sandbox Code Playgroud)

您还可以设置对象title的 和所有其他属性configuration