Created
June 20, 2021 08:45
-
-
Save damodarnamala/b68c2907adf8f7b1f09de6dec3ff12e3 to your computer and use it in GitHub Desktop.
Design System
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
extension UIButton { | |
private func actionHandler(action:(() -> Void)? = nil) { | |
struct HandlerStruct { static var action :(() -> Void)? } | |
if action != nil { | |
HandlerStruct.action = action | |
} else { HandlerStruct.action?() } | |
} | |
@objc private func triggerActionHandler() { | |
self.actionHandler() | |
} | |
func actionHandler(controlEvents control: UIControl.Event, forAction action: @escaping () -> Void) { | |
self.actionHandler(action: action) | |
self.addTarget(self, action: #selector(triggerActionHandler), for: control) | |
} | |
} | |
public struct Design { | |
} | |
extension Design { | |
public struct Button { | |
public static let primaryButton: ContentStyle<UIButton> = { button in | |
button.backgroundColor = .systemBlue | |
button.tintColor = .white | |
button.layer.cornerRadius = ConerRadius.size(.small)() | |
} | |
public static let dangerButton: ContentStyle<UIButton> = { button in | |
button.setTitleColor(.red, for: .normal) | |
button.layer.cornerRadius = ConerRadius.size(.small)() | |
button.layer.borderWidth = 1 | |
button.layer.borderColor = UIColor.red.cgColor | |
button.backgroundColor = UIColor.red.withAlphaComponent(0.05) | |
} | |
public static let secondaryButton: ContentStyle<UIButton> = { button in | |
button.backgroundColor = .clear | |
button.tintColor = .blue | |
button.layer.cornerRadius = ConerRadius.size(.small)() | |
button.layer.borderWidth = 1 | |
button.layer.borderColor = UIColor.blue.cgColor | |
} | |
public enum Design { | |
case primary | |
case secondary | |
case danger | |
} | |
} | |
} | |
extension Design { | |
public struct Label { | |
public static let heading: ContentStyle<UILabel> = { label in | |
label.font = .preferredFont(forTextStyle: .body) | |
} | |
} | |
} | |
extension Design { | |
public enum ConerRadius { | |
case small | |
case medium | |
case big | |
fileprivate func size() -> CGFloat { | |
switch self { | |
case .small: | |
return 8 | |
case .medium: | |
return 16 | |
case .big: | |
return 32 | |
} | |
} | |
} | |
} | |
class View: UIView { | |
public var cornerRadius : Design.ConerRadius? { | |
didSet(value) { | |
guard let r = cornerRadius else { return } | |
layer.cornerRadius = r.size() | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} | |
public typealias ContentStyle<Content> = (Content) -> Void | |
class Button: UIButton { | |
public var style : Design.Button.Design? { | |
didSet(value) { | |
applayStyle() | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
init(style: Design.Button.Design) { | |
super.init(frame: .zero) | |
self.style = style | |
self.applayStyle() | |
} | |
private func applayStyle() { | |
switch style { | |
case .primary: | |
Design.Button.primaryButton(self) | |
case .danger: | |
Design.Button.dangerButton(self) | |
case .secondary: | |
Design.Button.secondaryButton(self) | |
case .none: | |
print("=--") | |
} | |
} | |
} | |
extension Design { | |
struct Color { | |
public static let primary: ContentStyle<UIColor> = { color in | |
} | |
public static let accent: ContentStyle<UIColor> = { color in | |
} | |
public static let seconary: ContentStyle<UIColor> = { color in | |
} | |
} | |
} | |
/// Usage: | |
class ViewController: UIViewController { | |
// @IBOutlet weak var backView: View! | |
// @IBOutlet weak var button: Button! | |
// | |
lazy var danger: Button = { | |
let btn = Button(style: .danger) | |
btn.setTitle("HOME", for: .normal) | |
btn.translatesAutoresizingMaskIntoConstraints = false | |
return btn | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.addSubview(danger) | |
danger.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -100).isActive = true | |
danger.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true | |
danger.widthAnchor.constraint(equalToConstant: 100).isActive = true | |
danger.heightAnchor.constraint(equalToConstant: 54).isActive = true | |
// backView.cornerRadius = .big | |
// button.style = .secondary | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment