Skip to content

Instantly share code, notes, and snippets.

@yoching
Created September 7, 2017 07:35
Show Gist options
  • Save yoching/de20c48cdebd4be25f13d1966cfdae99 to your computer and use it in GitHub Desktop.
Save yoching/de20c48cdebd4be25f13d1966cfdae99 to your computer and use it in GitHub Desktop.
FunctionalViewStyleExperiments2
//: Playground - noun: a place where people can play
import UIKit
import UIKit
import XCPlayground
import PlaygroundSupport
let viewController = UIViewController()
viewController.view.backgroundColor = .white
let view = UIView()
view.backgroundColor = .orange
view.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
viewController.view.addSubview(view)
PlaygroundPage.current.liveView = viewController
struct Style {
typealias Apply = (UIView) -> UIView
let apply: Apply
init() {
apply = { view in
return view
}
}
init(apply: @escaping Apply) {
self.apply = apply
}
}
extension Style {
func compose(
apply apply1: @escaping Apply,
with apply2: @escaping Apply
) -> Apply {
return { view in
return apply2(apply1(view))
}
}
func newApplyAdded(_ newApply: @escaping Apply) -> Style {
return Style(apply: compose(apply: apply, with: newApply))
}
func cornerRadius(_ radius: CGFloat) -> Style {
let cornerRadiusApply: Apply = { view in
view.layer.cornerRadius = radius
return view
}
return newApplyAdded(cornerRadiusApply)
}
func borderWidth(_ width: CGFloat) -> Style {
let borderWidthApply: Apply = { view in
view.layer.borderWidth = width
return view
}
return newApplyAdded(borderWidthApply)
}
func borderColor(_ color: CGColor) -> Style {
let borderColorApply: Apply = { view in
view.layer.borderColor = color
return view
}
return newApplyAdded(borderColorApply)
}
}
let finalStyle = Style()
.borderWidth(10.0)
.cornerRadius(10.0)
.borderColor(UIColor.blue.cgColor)
finalStyle
.apply(view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment