Swift3 如何使用swift更改UILabel的背景色

Swift3 如何使用swift更改UILabel的背景色,swift3,label,xcode8,background-color,Swift3,Label,Xcode8,Background Color,在我的Xcode项目中,我想使用Swift更改/设置标签的背景色。我怎么能简单地做到这一点 我有这个,但它只适用于按钮 label.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0) 你可以这样做 label.layer.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0).cg

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

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

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

你可以这样做

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

1] 如果要更改UILabel背景色,请使用以下简单代码行:

yourLabelName.backgroundColor = UIColor.green
   yourLabelName.textColor = UIColor.red
注意:-您还可以使用不同类型的标准颜色,UIColor.white、UIColor.red

2] 如果要更改UILabel文本颜色,请使用以下简单代码行:

yourLabelName.backgroundColor = UIColor.green
   yourLabelName.textColor = UIColor.red

//喜欢编码

SWIFT 4.0/3.0:

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

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

更改UILabel背景色:

<your label name>.backgroundColor = UIColor.green
.backgroundColor=UIColor.green
更改UILabel文本颜色

<your label name>.textColor = UIColor.green
.textColor=UIColor.green

这是可行的,但问题是我的标签尺寸大于文本,我只想更改文本背景颜色