【问题标题】:Button subtitle label按钮字幕标签
【发布时间】:2023-02-15 18:43:54
【问题描述】:

有一天,我在情节提要中做了带有标题标签、副标题标签和图像的按钮,得到了这种效果

Image of button in storyboard

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

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
    }()

【问题讨论】:

    标签: swift button


    【解决方案1】:

    您需要创建一个 UIButton.Configuration 并在 configuration 对象上设置 subtitle

    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
    

    您也可以改为在 configuration 对象上设置 title 和所有其他属性。

    【讨论】:

      【解决方案2】:

      尝试以下技巧将帮助您更改按钮的副标题。

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

      查看here 以获得详细答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-14
        • 1970-01-01
        相关资源
        最近更新 更多