// // Line.swift // VDS // // Created by Nadigadda, Sumanth on 24/03/23. // import Foundation import UIKit import VDSCoreTokens /// A line visually separates content sections or elements in lists, tables and layouts to indicate content hierarchy. @objcMembers @objc(VDSLine) open class Line: View { //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero) } public override init(frame: CGRect) { super.init(frame: .zero) } public required init?(coder: NSCoder) { super.init(coder: coder) } //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- /// Enum used to describe the style the line that will be displayed. public enum Style: String, CaseIterable { case primary, secondary } /// Enum used to describe the orientation that will be displayed. public enum Orientation: String, CaseIterable { case horizontal, vertical } //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- /// Style of the line that will be displayed. open var style: Style = .primary { didSet { setNeedsUpdate() } } /// Allows to render the line vertically. open var orientation: Orientation = .horizontal { didSet { setNeedsUpdate() } } /// The natural size for the receiving view, considering only properties of the view itself. open override var intrinsicContentSize: CGSize { if orientation == .vertical { return .init(width: lineWidth, height: bounds.height) } else { return .init(width: bounds.width, height: lineWidth) } } //-------------------------------------------------- // MARK: - Configuration //-------------------------------------------------- /// Width of the line. public let lineWidth: CGFloat = 1.0 /// Line Color for current Style and Surface. open var lineColor: UIColor { lineViewColorConfiguration.getColor(self) } /// Color configuration use for text color. This is a configuration that will provide a color based /// of local properties such as style and surface. open var lineViewColorConfiguration: AnyColorable = { let config = KeyedColorConfiguration(keyPath: \.style) config.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forKey: .primary) config.setSurfaceColors(VDSColor.elementsLowcontrastOnlight, VDSColor.elementsLowcontrastOndark, forKey: .secondary) return config.eraseToAnyColorable() }() //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. open override func setup() { super.setup() } /// Used to make changes to the View based off a change events or from local properties. open override func updateView() { super.updateView() setNeedsDisplay() } /// Resets to default settings. open override func reset() { super.reset() style = .primary orientation = .horizontal } open override func draw(_ rect: CGRect) { guard let context = UIGraphicsGetCurrentContext() else { return } context.setStrokeColor(lineColor.cgColor) context.setLineWidth(lineWidth) if orientation == .horizontal { context.move(to: CGPoint(x: 0, y: rect.height / 2)) context.addLine(to: CGPoint(x: rect.width, y: rect.height / 2)) } else { context.move(to: CGPoint(x: rect.width / 2, y: 0)) context.addLine(to: CGPoint(x: rect.width / 2, y: rect.height)) } context.strokePath() } }