vds_ios_sample/VDSSample/ViewControllers/LabelViewController.swift
Matt Bruce 4b03f39443 added Toggle/ToggleItem
update Toggle in screens to not use a wrapper

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-07-19 09:41:28 -05:00

216 lines
8.3 KiB
Swift

//
// LabelViewController.swift
// VDSSample
//
// Created by Matt Bruce on 8/16/22.
//
import Foundation
import UIKit
import VDS
import VDSColorTokens
class LabelViewController: BaseViewController<Label> {
lazy var textSizePickerSelectorView = {
TextSizePickerSelectorView(title: "",
picker: self.picker)
}()
lazy var textPositionPickerSelectorView = {
PickerSelectorView(title: TextPosition.left.rawValue,
picker: self.picker,
items: TextPosition.allCases)
}()
lazy var fontCategoryPickerSelectorView = {
FontCategoryPickerSelectorView(title: "",
picker: self.picker)
}()
var lineheightRange = Slider()
var lineSpacingRange = Slider()
var baselineOffsetRange = Slider()
var currentStyle: TextStyle!
var boldSwitch = Toggle()
var disabledSwitch = Toggle()
var textField = TextField()
private var isBold: Bool = false
var lineHeightLabel = UILabel().with { $0.translatesAutoresizingMaskIntoConstraints = false; $0.lineBreakMode = .byWordWrapping; $0.numberOfLines = 0}
override func viewDidLoad() {
super.viewDidLoad()
let stack = UIStackView()
stack.axis = .vertical
stack.spacing = 10
stack.addArrangedSubview(component)
stack.addArrangedSubview(lineHeightLabel)
addContentTopView(view: stack)
setupPicker()
setupModel()
}
var defaultLineHeightLabel: UILabel?
override func allTextFields() -> [TextField]? { [textField] }
override func setupForm(){
super.setupForm()
addFormRow(label: "Disabled", view: disabledSwitch)
addFormRow(label: "Bold", view: boldSwitch)
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Text Position", view: textPositionPickerSelectorView)
addFormRow(label: "Font Category", view: fontCategoryPickerSelectorView)
addFormRow(label: "Text Size", view: textSizePickerSelectorView)
let view = addFormRow(label: "Line Height", view: lineheightRange)
addFormRow(label: "Line Spacing", view: lineSpacingRange)
addFormRow(label: "Baseline Offset", view: baselineOffsetRange)
defaultLineHeightLabel = view.viewWithTag(1) as? UILabel
addFormRow(label: "Text", view: textField)
disabledSwitch.onChange = { [weak self] sender in
self?.component.disabled = sender.isOn
}
boldSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.isBold = sender.isOn
self?.updateLabelStyle()
}.store(in: &subscribers)
textField
.textPublisher
.sink { [weak self] text in
self?.component.text = text
self?.lineHeightLabel.text = text
}.store(in: &subscribers)
lineheightRange.maximumValue = 20.0
lineheightRange.minimumValue = 0.0
lineheightRange.value = 2.0
lineheightRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
guard let self else { return }
let textStyle = self.component.textStyle
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
fontFace: textStyle.fontFace,
pointSize: textStyle.pointSize,
lineHeight: CGFloat(slider.value),
letterSpacing: textStyle.letterSpacing)
}).store(in: &subscribers)
lineSpacingRange.maximumValue = 80.0
lineSpacingRange.minimumValue = 0.0
lineSpacingRange.value = 0.0
lineSpacingRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
guard let self else { return }
let textStyle = self.component.textStyle
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
fontFace: textStyle.fontFace,
pointSize: textStyle.pointSize,
lineHeight: textStyle.lineHeight,
letterSpacing: textStyle.letterSpacing,
lineSpacing: CGFloat(slider.value),
baselineOffset: textStyle.baselineOffset
)
}).store(in: &subscribers)
baselineOffsetRange.maximumValue = 100
baselineOffsetRange.minimumValue = -100
baselineOffsetRange.value = 0.0
baselineOffsetRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
guard let self else { return }
let textStyle = self.component.textStyle
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
fontFace: textStyle.fontFace,
pointSize: textStyle.pointSize,
lineHeight: textStyle.lineHeight,
letterSpacing: textStyle.letterSpacing,
lineSpacing: textStyle.lineSpacing,
baselineOffset: CGFloat(slider.value)
)
}).store(in: &subscribers)
}
func setupModel() {
component.text = "Label Component"
component.textStyle = .featureSmall
lineHeightLabel.text = component.text
lineHeightLabel.font = component.textStyle.font
//setup UI
surfacePickerSelectorView.text = component.surface.rawValue
disabledSwitch.isOn = component.disabled
boldSwitch.isOn = isBold
textField.text = component.text
//set the font
fontCategory = .feature
fontCategoryPickerSelectorView.text = "Feature"
textSize = .small
textSizePickerSelectorView.text = "Small"
updateLabelStyle()
}
override func showDebug(show: Bool) {
super.showDebug(show: show)
lineHeightLabel.debugBorder(show: show)
}
//Picker
private var fontCategory: TextStyle.FontCategory = .feature {
didSet {
fontCategoryPickerSelectorView.text = fontCategory.rawValue
textSizePickerSelectorView.items = fontCategory.sizes
if textSizePickerSelectorView.items.count > 0 {
textSize = textSizePickerSelectorView.items[0]
} else {
textSize = nil
}
updateLabelStyle()
}
}
private var textSize: TextStyle.FontSize? = .large {
didSet {
textSizePickerSelectorView.text = textSize?.rawValue ?? ""
updateLabelStyle()
}
}
func updateLabelStyle(){
if let style = fontCategory.style(for: textSize, isBold: isBold) {
component.textStyle = style
currentStyle = style
let defaultLineHeight = style.font.lineHeight
lineHeightLabel.font = style.font
lineheightRange.maximumValue = Float(defaultLineHeight) * 1.50
lineheightRange.minimumValue = Float(defaultLineHeight) * 0.50
lineheightRange.value = Float(style.lineHeight)
defaultLineHeightLabel?.text = "Line Height - d:\(defaultLineHeight) s:\(currentStyle.lineHeight)"
}
}
func setupPicker(){
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.surface = item
self?.contentTopView.backgroundColor = item.color
}
textPositionPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.textPosition = item
}
textSizePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.textSize = item
}
fontCategoryPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.fontCategory = item
}
}
}