Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Project Mess #3: Hardcoded UI colours

by fragi
February 19, 2022
in Project Mess
Share on FacebookShare on Twitter

In this short article we will see another project mess.
Let’s see the following example:

Swift
1
2
3
4
5
6
7
8
9
10
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .foregroundColor(Color.black)
                .padding(.leading, Spacing.tight.rawValue)
                .background(Color.red)
        }
    }
}

A worst variation of this will be a color initialised with hex values.
Using hard coded values for colours will not get us far. And the day we will be requested to change all the buttons background colour is not far.
Let’s see how we can improve this. As we did with the Spacing, we need a central place for these values.

Swift
1
2
3
4
5
6
public class ColorTheme {
    enum Palette {
        static var text = Color.black
        static var background = Color.red
    }
}

So we can update our view code:

Swift
1
2
3
4
5
6
7
8
9
10
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .foregroundColor(ColorTheme.Palette.text)
                .padding(.leading, Spacing.tight.rawValue)
                .background(ColorTheme.Palette.background)
        }
    }
}

fragi

fragi

Related Posts

Project Mess

Multiple Equatable methods on different Packages – Bad practise

May 24, 2023

In this article we will explore what happens when we have different equatable methods for a type across packages and...

Project Mess #2 – Magic numbers for UI spacing
Project Mess

Project Mess #2 – Magic numbers for UI spacing

February 18, 2022

In this short article we will address one common issue that can lead to a project mess is when all...

Project Mess #1: No Localised Strings.
Project Mess

Project Mess #1: No Localised Strings.

May 13, 2021

Every app has some static text. Either as a title of a screen or as a title of a button,...

Next Post
Extract build settings – Xcode

Extract build settings - Xcode

Postman – Mock server

Postman - Mock server

Postman – Schema Validation

Postman - Schema Validation

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.