Suyog
Suyog

Reputation: 133

Is there a way to use a colour scheme on an application without having to individually write the colour values on each part of the code?

I am developing a Single View application with multiple screens. I want to make use of multiple shades of blue within the application. Right now I'm accessing the UIColor/cgColor property of each view within the multiple viewControllers to assign a particular colour. eg:

    cell.layer.borderColor = UIColor.init(red: 0/255, green: 178/255, blue: 1, alpha: 0.5).cgColor

and,

    cell.backgroundColor = UIColor.init(red: 0/255, green: 128/255, blue: 1, alpha: 1)

and,

    tableView.backgroundColor = UIColor.white

I wish to know if there is a way to define all the colours without multiple lines of codes all across the multiple view controllers.

Upvotes: 1

Views: 67

Answers (3)

Nikola Ristic
Nikola Ristic

Reputation: 449

It seems that the best way would be to create color extension:

extension UIColor {
    static var borderColor: UIColor {
        return UIColor.init(red: 0/255, green: 178/255, blue: 1, alpha: 0.5)
    }
    static var backgroundColor: UIColor {
        return UIColor.init(red: 0/255, green: 128/255, blue: 1, alpha: 1)
    }
}

and then use it:

cell.layer.borderColor = UIColor.borderColor.cgColor
cell.layer.backgroundColor = UIColor.backgroundColor.cgColor

For more general approach you can use UIAppearance protocol, eg. for your case you would create extension

extension UITableViewCell {

    @objc public var borderColor: UIColor? {
        set { layer.borderColor = newValue?.cgColor }
        get { return layer.borderColor.map({ UIColor(cgColor: $0)}) }
    }

    @objc public var layerBackgroundColor: UIColor? {
        set { layer.backgroundColor = newValue?.cgColor }
        get { return layer.backgroundColor.map({ UIColor(cgColor: $0)}) }
    }
}

and then use it in code:

UITableViewCell.appearance().borderColor = UIColor.borderColor
UITableViewCell.appearance().layerBackgroundColor = UIColor.backgroundColor

For more details you can take a look at: UIAppearance and Styling your app using custom UIAppearance properties

Upvotes: 0

Thiago Valente
Thiago Valente

Reputation: 703

You can create a struct to create static values

struct AppThemeColor{
    static func cellLayerColor() -> UIColor{
        return UIColor.init(red: 0/255, green: 178/255, blue: 1, alpha: 0.5)
    }

    static func cellBackgroundColor() -> UIColor{
        return UIColor.init(red: 0/255, green: 128/255, blue: 1, alpha: 1)
    }
    // ...
}

To get the color is simples

cell.backgroundColor = AppThemeColor.cellBackgroundColor()

Upvotes: 0

Pavel Kozlov
Pavel Kozlov

Reputation: 1003

I think you're looking for appearance proxy. Please refer to documentation on how you can use it, like:

UITableView.appearance().backgroundColor = .green

You would probably also want to extend UIColor with your custom colors like:

extension UIColor {
    class var customColor: UIColor {
        return UIColor.init(red: 0/255, green: 178/255, blue: 1, alpha: 0.5)
    }
}

Upvotes: 2

Related Questions