HowTo: Integration with Other Libraries - e2technologies/ViewCSS GitHub Wiki
There is sometimes a need to interact with other libraries that extend UIView elements doing a similar thing that ViewCSS does. For example, the "Font Awesome" library FAIcon provides methods to set icons in UILabels (among other things). These kinds of libraries do not play to well with ViewCSS. However, there is a workaround.
Lets take the FAIcon method "setFAIcon" and "setFATitleColor " as an example. The following code will set the UIButton to have the "arrow-up" arrow with the specified color.
class MyCustomView: UIView {
@IBOutlet weak var button: UIButton?
override func awakeFromNib() {
super.awakeFromNib()
self.button?.setFAIcon(icon: .FAArrowUp, forState: .normal)
self.button?.setFATitleColor(color: UIColor.red)
}
}
Normally with ViewCSS, you would add the ".css" call AFTER this code so that the color can be overridden (shown below). However this clears out the "arrow-up" icon and places it with a "?" signaling the reference is unknown.
class MyCustomView: UIView {
@IBOutlet weak var button: UIButton?
override func awakeFromNib() {
super.awakeFromNib()
self.button?.setFAIcon(icon: .FAArrowUp, forState: .normal)
self.button?.setFATitleColor(color: UIColor.red)
self.css(object: self.button, class: "button")
}
}
The fix for this is to use the CSS callback to configure the FAIcon AFTER the CSS is applied. This is shown below
class MyCustomView: UIView {
@IBOutlet weak var button: UIButton?
override func awakeFromNib() {
super.awakeFromNib()
self.css(object: self.button, class: "button") { (config: ViewCSSConfig) in
self.button?.setFAIcon(icon: .FAArrowUp, forState: .normal)
self.button?.setFATitleColor(color: config.color ?? UIColor.red)
}
}
}