【问题标题】:How to change the background color of a UILabel with swift如何使用 swift 更改 UILabel 的背景颜色
【发布时间】:2017-07-06 09:41:00
【问题描述】:

在我的 Xcode 项目中,我想使用 Swift 更改/设置标签的背景颜色。我怎样才能简单地做到这一点?

我有这个,但它只适用于按钮。

label.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0)

【问题讨论】:

    标签: swift3 label xcode8 background-color


    【解决方案1】:

    为 swift 3 更新:

    1]如果你想改变UILabel的背景颜色,那么用下面几行简单的代码:

    yourLabelName.backgroundColor = UIColor.green
    

    注意:-你也可以使用不同类型的标准颜色,UIColor.white,UIColor.red...

    2]如果要改变UILabel的文字颜色,那么用下面几行简单的代码:

       yourLabelName.textColor = UIColor.red
    

    // 享受编码...!

    【讨论】:

      【解决方案2】:

      你可以这样做

      label.layer.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0).cgColor
      

      【讨论】:

      • 这行得通,但对我来说问题是我的标签大小大于文本,我只想更改文本背景颜色
      【解决方案3】:

      SWIFT 4.0/3.0:

      <your label name>.backgroundColor = UIColor.green
      

      SWIFT 2.0:

      <your label name>.backgroundColor = UIColor.green()
      

      【讨论】:

        【解决方案4】:

        更改 UILabel 背景颜色:

        <your label name>.backgroundColor = UIColor.green
        

        更改 UILabel 文本颜色

        <your label name>.textColor = UIColor.green
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 2010-09-18
          • 1970-01-01
          • 1970-01-01
          • 2020-01-26
          相关资源
          最近更新 更多