Created
March 7, 2019 01:03
-
-
Save bolivarbryan/54cfb7edcf76498252c98bc82652094e to your computer and use it in GitHub Desktop.
Diamond Operator: reusability and styling
This file contains hidden or 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 | |
precedencegroup SingleTypeComposition { | |
associativity: right | |
} | |
infix operator <>: SingleTypeComposition | |
func <> <A: AnyObject>(f: @escaping (A) -> Void, g: @escaping (A) -> Void) -> (A) -> Void { | |
return { a in | |
f(a) | |
g(a) | |
} | |
} | |
let backgroundColor = UIColor.lightGray | |
let borderColor = UIColor.gray | |
enum FontSize: CGFloat { | |
case big = 30.0 | |
case normal = 20 | |
case small = 15 | |
case tiny = 12 | |
} | |
func styleViewBackground(color: UIColor) -> (UIView) -> Void { | |
return { | |
$0.backgroundColor = color | |
} | |
} | |
func styleViewBorder(color: UIColor, width: CGFloat) -> (UIView) -> Void { | |
return { | |
$0.layer.borderColor = color.cgColor | |
$0.layer.borderWidth = width | |
} | |
} | |
let styleViewCircleBase: (UIView) -> Void = | |
styleViewBackground(color: backgroundColor) | |
<> { | |
$0.layer.cornerRadius = $0.frame.width * 0.5 | |
$0.layer.masksToBounds = true | |
} | |
let styleViewCircleBorder: (UIView) -> Void = | |
styleViewCircleBase | |
<> styleViewBorder(color: borderColor, width: 4) | |
let styleLabelBase: (UILabel) -> Void = { | |
$0.textAlignment = .center | |
$0.textColor = .white | |
$0.numberOfLines = 0 | |
} | |
let styleLabelBigFont: (UILabel) -> Void = { | |
$0.font = UIFont.systemFont(ofSize: FontSize.big.rawValue, weight: .bold) | |
} | |
let styleLabelCircle: (UILabel) -> Void = | |
styleLabelBase | |
<> styleViewCircleBorder | |
<> styleLabelBigFont | |
let styleLabelCheckMark: (UILabel) -> Void = | |
styleLabelCircle | |
<> { | |
$0.text = "√" | |
} | |
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 800)) | |
view.backgroundColor = .white | |
let borderCircle = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) | |
styleViewCircleBorder(borderCircle) | |
view.addSubview(borderCircle) | |
let label = UILabel(frame: CGRect(x: 50, y: 170, width: 80, height: 80)) | |
label.text = "B" | |
styleLabelCircle(label) | |
view.addSubview(label) | |
let checkLabel = UILabel(frame: CGRect(x: 50, y: 290, width: 200, height: 200)) | |
styleLabelCheckMark(checkLabel) | |
view.addSubview(checkLabel) | |
PlaygroundPage.current.liveView = view | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment