Working with UIColor and Array

Dears,


It's my first project using Swift, and I'm completely lost...


I have a list of itens to be shown in 10 UILabels... each of these itens will have a differente color, depending on the user's progress.

So, I've created an Array of strings to populate all of this UILabels with a simple For loop...


Now, my question is:

Once I've three different type of "PROGRESS", is it possible to create a function to define the text colors in my array?


It would be something like:


Let funcText: [String] = [

("Text1", progressLoadingColor),

("Text1", progressCompletedColor),

("Text1", progressDelayedColor),

]


Is it possible to be done?

Should I use NSAttributedString?

Accepted Reply

If the whole lable is in the same color, no need for attributes string.


Just :

label.text = theText

label.textColor = .red // the color


It would be easier to help if you showed your code.


So, in your case, you have probably defined a collection for IBOutlets

    @IBOutlet var labels: [UILabel]!


So, you have an array

let arrayOfString = ["Label1", "Label2"] // etc til 10

and set the value:


for i in 0..<labels.count {

labels[i].text = arrayOfString[i]

}


I understand that user has a progress level

var userLevel : Int = 0 // At beginning, then 1, 2…


then you may have defined 3 colors, for each level

let levelsColors : [UIColor] = [.green, .orange, .red]


then, you set the text color for a label (label i) :

labels[i].textColor = levelsColors[userLevel]

Replies

If the whole lable is in the same color, no need for attributes string.


Just :

label.text = theText

label.textColor = .red // the color


It would be easier to help if you showed your code.


So, in your case, you have probably defined a collection for IBOutlets

    @IBOutlet var labels: [UILabel]!


So, you have an array

let arrayOfString = ["Label1", "Label2"] // etc til 10

and set the value:


for i in 0..<labels.count {

labels[i].text = arrayOfString[i]

}


I understand that user has a progress level

var userLevel : Int = 0 // At beginning, then 1, 2…


then you may have defined 3 colors, for each level

let levelsColors : [UIColor] = [.green, .orange, .red]


then, you set the text color for a label (label i) :

labels[i].textColor = levelsColors[userLevel]

Hello Claude31, how are you?


Thank you so much.

As I mentioned before, it's my first time using Swift, all my background is using VBA... so, I'm completely lost.


I've no code set yet, but basically what I would like to do is:

I have 10 UILabels in my UIView, and depending on button selected by the user (I will have around 20 different buttons), the system will fill up those 10 UILabels with some information... Each of the UILabels will have a different color, based on the label category.


So, once I have 20 buttons, each one with 10 different options to be show, I woudn't like to have to define a text color everytime a text is inserted into the label...

And based on the labels category, I've only 3 different category, so I would like to create something like a func, or a struct, that would allow me to define the text color while I'll be defining the array.


like

Page1 = [

"Bruno", .onGoing,

"Joseph", .done,

"Marta", .waitingforApproval,

]


And onGoing is green color, with bold text type

Done is blue with bold

waitinforApproval is yellow with italic...


Did you understand now?


I know it's difficult for you to understand my question. It's my first time using Swift... I'm so sorry for that.

Thank you so much

No, it is not totally clear ; would you have

1. text with multi colors and different style in a given label ?

"Bruno Joseph Martha", with Bruno green bold, Joseph blue bold and Martha red italic

2. Or is all text in one label of the same color and same style?

"Bruno" green bold


if 1, then you need to use attributedText

if 2, do as I wrote before.


An advice : start simple, don't try to do everything at once, this is why you feel lost. Go incrementally!

firstly, write just plain text in the labels.

Once you have it working, with the right content, you will improve with style and color.

And you will be able to post some code, in a new thread, if you need advice.

Thanks again.


The whole text will be on the same color.


But let my try to make clear for you.

I'll have around 20 different arrays.

Each array will have around 10 different elements.


The elements of this array will be shown on those 10 UILabels of my UiView.


Is there any way to create 3 different types of "something" to be used in this array?


Like:

Type 1 red and bold text.

Type 2 with yellow and italic text and

Type 3 with black and normal text


So, I'll be able to use it while declaring my arrays, insted of having to write the code you provided everytime I'm filling up the UILabels.


Example:


let array1: String = [

"Bruno", Type1

"Julio", Type2

"MNaria", Type3

]



Is it possible?

You can write something like this:


struct LabelTheme {
    let color: UIColor
    let font: UIFont
    
    static let type1 = LabelTheme(color: .red, font: UIFont.boldSystemFont(ofSize: 12))
    static let type2 = LabelTheme(color: .yellow, font: UIFont.italicSystemFont(ofSize: 12))
    static let type3 = LabelTheme(color: .black, font: UIFont.systemFont(ofSize: 12))
    
    func set(to label: UILabel) {
        label.textColor = color
        label.font = font
    }
}

struct LabelContent {
    let text: String
    let theme: LabelTheme
    
    func set(to label: UILabel) {
        label.text = text
        theme.set(to: label)
    }
}

let array1 = [
    LabelContent(text: "Bruno", theme: .type1),
    LabelContent(text: "Julio", theme: .type1),
    LabelContent(text: "MNaria", theme: .type1),
]


Use it as:

    array1[0].set(to: labelA)


Or, you can define an extension in addition:

extension UILabel {
    var theme: LabelTheme {
        get {
            return LabelTheme(color: textColor, font: font)
        }
        set {
            textColor = newValue.color
            font = newValue.font
        }
    }
    
    var content: LabelContent {
        get {
            return LabelContent(text: text ?? "", theme: theme)
        }
        set {
            text = newValue.text
            theme = newValue.theme
        }
    }
}

And use the `array1` above as:

    labelA.content = array1[1]

That's exactly what I was looking for.

Thank you so much!