Created
October 2, 2018 00:46
-
-
Save stzn/c269859bbada5048ff1877c64df496f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
struct Decorator<View: UIView> { | |
let decorate: (View) -> View | |
} | |
func decorate<View: UIView>(_ view: View, with decorator: Decorator<View>) -> View { | |
return decorator.decorate(view) | |
} | |
func decorate<View: UIView>(_ view: View, with decorators: [Decorator<View>]) -> View { | |
for decorator in decorators { | |
decorator.decorate(view) | |
} | |
return view | |
} | |
func cornerRadious<View: UIView>(cornerRadius: CGFloat) -> Decorator<View> { | |
return Decorator { view in | |
view.layer.cornerRadius = cornerRadius | |
return view | |
} | |
} | |
func shadow<View: UIView>(radius: CGFloat) -> Decorator<View> { | |
return Decorator { view in | |
view.layer.shadowRadius = radius | |
view.layer.shadowOpacity = 0.3 | |
view.layer.shadowColor = UIColor.black.cgColor | |
view.layer.shadowOffset = CGSize(width: 0, height: radius / 2) | |
view.layer.masksToBounds = false | |
return view | |
} | |
} | |
func background<View: UIView>(color: UIColor) -> Decorator<View> { | |
return Decorator { view in | |
view.backgroundColor = color | |
return view | |
} | |
} | |
func buttonTitle<View: UIButton>(_ title: String) -> Decorator<View> { | |
return Decorator { button in | |
button.setTitle(title, for: .normal) | |
return button | |
} | |
} | |
func buttonTitleColor<View: UIButton>(_ color: UIColor) -> Decorator<View> { | |
return Decorator { button in | |
button.setTitleColor(color, for: .normal) | |
return button | |
} | |
} | |
func okButton(frame: CGRect) -> UIView { | |
let button = UIButton(frame: frame) | |
return decorate(button, with:[ | |
buttonTitle("OK"), | |
buttonTitleColor(.white), | |
// background(color: .black) // ここをどうにかしたい | |
]) | |
} | |
func cardView(frame: CGRect) -> UIView { | |
let view = UIView(frame: frame) | |
return decorate(view, with: [ | |
background(color: .red), | |
cornerRadious(cornerRadius: 2), | |
shadow(radius: 4) | |
]) | |
} | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) | |
self.view.backgroundColor = .white | |
let card = cardView(frame: CGRect(x: 100, y: 100, width: 80, height: 80)) | |
self.view.addSubview(card) | |
let ok = okButton(frame: CGRect(x: 100, y: 200, width: 50, height: 50)) | |
self.view.addSubview(ok) | |
} | |
} | |
let viewController = ViewController() | |
PlaygroundPage.current.liveView = viewController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment