- Import the framework
import Outfit
- Define conformance of your class to
Outfitable
protocol
extension MyCustomView: Outfitable {}
- Extend your wardrobe with some nice outfit
extension Wardrobe where Wearer: MyCustomView {
func perfectLook() -> Outfit<Wearer> {
return {
// Customize the instance of MyCustomView
$0.titleColor = .red
$0.placeholder = "It's something new"
$0.clipsToBounds = true
}
}
}
- Access your wardrobe and try it on
let view = MyCustomView()
view.wrd.tryOn {
$0.perfectLook()
}
- Have fun!
You can combine outfits together using custom concat operator (++)
extension Wardrobe where Wearer: UIView {
func rounded(_ radius: CGFloat) -> Outfit<Wearer> {
return {
$0.layer.masksToBounds = true
$0.layer.cornerRadius = radius
}
}
func background(_ color: UIColor) -> Outfit<Wearer> {
return { $0.backgroundColor = color }
}
func defaultView() -> Outfit<Wearer> {
return rounded(5.0)
++ background(.white)
}
}
You can reuse outfits from parent class in the child
extension Wardrobe where Wearer: UIView {
func defaultView() -> Outfit<Wearer> { ... }
}
extension Wardrobe where Wearer: UIButton {
func title(_ text: String) -> Outfit<Wearer> {
return {
$0.setTitle(text, for: .normal)
}
}
func defaultButton() -> Outfit<Wearer> {
return defaultView()
++ title("Default title")
}
}
If you need to override some property in a single place or add new one, and it doesn't make a sense to define new function for this:
func viewDidLoad() {
super.viewDidLoad()
let view = MyCustomView()
view.wrd.tryOn {
$0.perfectLook()
++ { $0.titleColor = .green }
}
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Swift 4.2
- Xcode 10
Outfit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Outfit'
Oleksii Faizullov, [email protected]
Outfit is available under the MIT license. See the LICENSE file for more info.