104 lines
2.6 KiB
Swift
104 lines
2.6 KiB
Swift
//
|
|
// VDSLabel.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 7/28/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import Combine
|
|
|
|
open class DefaultLabelModel: VDSLabelModel {
|
|
public var fontCategory: VDSFontCategory = .body
|
|
public var fontSize: VDSFontSize = .large
|
|
public var fontWeight: VDSFontWeight = .regular
|
|
public var textPosition: VDSTextPosition = .left
|
|
public var surface: Surface = .light
|
|
required public init(){}
|
|
}
|
|
|
|
open class VDSLabel: UILabel, Modelable, ObservableObject {
|
|
@Published public var model: VDSLabelModel = DefaultLabelModel()
|
|
private var cancellable: AnyCancellable?
|
|
|
|
public var fontSize: VDSFontSize = .large {
|
|
didSet {
|
|
if fontSize != model.fontSize {
|
|
model.fontSize = fontSize
|
|
}
|
|
}
|
|
}
|
|
|
|
public var textPosition: VDSTextPosition = .left{
|
|
didSet {
|
|
if textPosition != model.textPosition {
|
|
model.textPosition = textPosition
|
|
}
|
|
}
|
|
}
|
|
|
|
public var fontWeight: VDSFontWeight = .regular {
|
|
didSet {
|
|
if fontWeight != model.fontWeight {
|
|
model.fontWeight = fontWeight
|
|
}
|
|
}
|
|
}
|
|
|
|
public var fontCategory: VDSFontCategory = .body {
|
|
didSet {
|
|
if fontCategory != model.fontCategory {
|
|
model.fontCategory = fontCategory
|
|
}
|
|
}
|
|
}
|
|
|
|
public var surface: Surface = .light {
|
|
didSet {
|
|
if surface != model.surface {
|
|
model.surface = surface
|
|
}
|
|
}
|
|
}
|
|
|
|
//Initializers
|
|
public convenience init() {
|
|
self.init(frame: .zero)
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
setup()
|
|
}
|
|
|
|
required public init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
setup()
|
|
}
|
|
|
|
func setup() {
|
|
cancellable = $model.sink { [weak self] viewModel in
|
|
self?.onStateChange(viewModel: viewModel)
|
|
}
|
|
}
|
|
|
|
//functions
|
|
private func onStateChange(viewModel: VDSLabelModel) {
|
|
textAlignment = viewModel.textPosition.textAlignment
|
|
textColor = viewModel.surface == .dark ? VDSColor.elementsPrimaryOndark : VDSColor.elementsPrimaryOnlight
|
|
|
|
guard let vdsFont = try? VDSFontStyle.font(for: viewModel.fontCategory, fontWeight: viewModel.fontWeight, fontSize: viewModel.fontSize) else {
|
|
font = VDSFontStyle.RegularBodyLarge.font
|
|
return
|
|
}
|
|
font = vdsFont
|
|
}
|
|
|
|
//Modelable
|
|
public func set(with model: VDSLabelModel) {
|
|
self.model = model
|
|
}
|
|
}
|