Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple theme patterns handling in iOS [closed]

What is the best approach to create multiple theme iOS application?

I want to create styling for the whole app. For example, each label can have customizable font, size, colors, etc.

Are there any libraries that can be used, and which design patterns would be good for this problem?

EDIT 1 - One use case is like we have different theme for different clients. how can we inject themes from a single place and that will reflect throughout the app. Need some code example for the same.

like image 203
Bojan Bosanac Avatar asked Apr 26 '26 02:04

Bojan Bosanac


1 Answers

As per your requirement, You want to inject different themes for different clients. You can use dependency injection in that case.

To understand dependency inject you can follow - https://medium.com/@JoyceMatos/dependency-injection-in-swift-87c748a167be

For your problem you can do something like below.

public protocol ThemeManagerProtocol {
    var color: UIColor?{ get set }
    var logoImageName: String?{ get set }
}


class ThemeManager {

    static var manager: ThemeManagerProtocol? = ClientOneThemeManager() //Need to pass from app delegate

    public class var color: UIColor? {
        return self.manager?.color
    }

    public class var logoImageName: String? {
        return self.manager?.logoImageName
    }
}

class Appdelegate {
    func applicationDidFinishLaunching(_ application: UIApplication) {
        ThemeManager.manager = ClientOneThemeManager()
    }
}


class ViewController: UIViewController {
    override func viewDidLoad() {
        print("\(ThemeManager.logoImageName)")
        print("\(ThemeManager.color.debugDescription)")

        //Above values will be picked from the respective client themes.
    }
}

class ClientOneThemeManager: ThemeManagerProtocol {
    var logoImageName: String? = "Image Client 1"
    var color: UIColor? = .red
}

class ClientTwoThemeManager: ThemeManagerProtocol {
    var logoImageName: String? = "Image Client 2"
    var color: UIColor? = .white
}
like image 193
Mohit Mangla Avatar answered Apr 27 '26 19:04

Mohit Mangla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!