How to change the background color of a UILabel with swift
Asked Answered
C

4

5

In my Xcode project, I want to change/set the background color of a label using Swift. How can I do that simply?

I have this, but it only works for buttons.

label.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0)
Cotopaxi answered 17/2, 2017 at 2:7 Comment(0)
C
12

Updated for swift 3:

1] if you want to change the UILabel background color, then used below simple lines of code:

yourLabelName.backgroundColor = UIColor.green

Note:- You can also be used different types of standard colors, UIColor.white, UIColor.red...

2] If you want to change the UILabel text color, then used below simple lines of code:

   yourLabelName.textColor = UIColor.red

// Enjoy Coding...!

Cholecystitis answered 31/8, 2017 at 10:10 Comment(0)
S
7

You can do that in this way

label.layer.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0).cgColor
Substitution answered 17/2, 2017 at 2:19 Comment(1)
this is working but the problem for me is that my label size in larger than text and I just want to change text background colorZorazorah
P
1

SWIFT 4.0/3.0:

<your label name>.backgroundColor = UIColor.green

SWIFT 2.0:

<your label name>.backgroundColor = UIColor.green()
Pharmacology answered 30/1, 2018 at 5:29 Comment(0)
G
0

Change the UILabel background color:

<your label name>.backgroundColor = UIColor.green

Change the UILabel text color

<your label name>.textColor = UIColor.green
Glen answered 24/1, 2019 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.