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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With