Search code examples
iosswiftcolorslabelgenerated

How to make generated text in a todo list come out colored


I am making a simple to do list tabbed app, where you type in a text box and it will generate the text on the second view controller. There are five buttons that are nearly identical but generate different colors. In the code below I'm just showing one. I have figured out almost everything I need and I know what to do (fish), just not how to word it in the coding language.

In my func for the button control, I have tried to add something such as "inputRed.textColor(this is where I'm confused and need help wording). I believe I am on the right track here, but I am not sure how to input the code.

import UIKit

class SecondViewController: UIViewController {


    @IBOutlet weak var inputRed: UITextField!

    var textColor: UIColor!

    @IBAction func inputRed(_ sender: Any) {

        if (inputRed.text != ""){
            list.append(inputRed.text!)
            inputRed.textColor(red:000, green:000, blue:000)
        }

    }
}

I've read somewhere you need to put "(red:000, green:000, blue:000)" with the zeros being the hex number, but it is giving me an error that says "Cannot invoke 'textColor' with an argument list of type '(red: Int, green: Int, blue: Int)'"

I have a feeling I am missing something to prefix the hex code but I could be completely wrong. I just need help putting what's in the parenthesis correctly or if I'm doing it completely wrong. Can someone help?

EDIT: I also get an additional error saying this: "Expected expression in list of expressions"


Solution

  • The textColor property expects a UIColor. You can use one of the predefined colors or you can create your own color.

    inputRed.textColor = .red // or any other color
    

    or:

    inputRed.textColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
    // or any other values in the range 0.0 - 1.0
    

    If you use 0 for red, green, and blue, that gives you the color black which can be done as:

    inputRed.textColor = .black