Overview - e2technologies/ViewCSS GitHub Wiki
ViewCSS is a CSS like plugin for iOS Applications. It provides a simple interface to define different attributes for UIView elements. It is intended to allow application developers/designers to enable CSS reuse methodologies as well as overriding the values at run-time. It is NOT intended to replace auto layout, NIBs, etc.
An example of use is shown below
import UIKit
import ViewCSS
class MyCustomViewController: UIViewController {
@IBOutlet weak var label1: UILabel?
@IBOutlet weak var label2: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
// Set the global CSS dictionary (see later section on this)
let css: [String:Any] = [
".bold" : [
"font-weight" : "bold"
],
"my_custom_view_controller.label1" : [
"font-size" : "16px",
"text-align": "left",
"color" : "red",
],
"my_custom_view_controller.label2" : [
"font-size" : "12px",
"text-align": "right",
"color" : "white",
],
]
ViewCSSManager.shared.setCSS(dict: css)
// ...
self.css(object: self.label1, class: "label1")
self.css(object: self.label2, class: "label2")
}
var setText(firstName: String, lastName: String) {
self.label1?.cssText = "\(firstName)<span class=\"bold\">\(lastName)</span>"
}
}
This will dynamically configure "label1" and "label2" with the defined settings in the "css" dictionary. Note that it only overrides the settings that were included in the dictionary, so any customizations that were done in code or a NIB will remain.
This enables common styles to be reused throughout the application. It also enables the attributes of UI elements to be changed outside of the compiled application. For example, the CSS dictionary could be a json file that is stored on a server somewhere that the application can periodically check for updates.
Since only the properties that are in the dictionary are modified, you can use this library purely as a way to override the default settings. In other words, you do not need to have every property defined in the dictionary, just those where you want to override a specific setting. This makes the use of this library NOT an "all or nothing" exercise. You can override the attributes on an "as needed" basis.