vds_ios_sample/VDSSample/ViewControllers/LabelViewController.swift
Matt Bruce 70beb503c7 rfactored to new textstyle naming
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-01-13 15:35:54 -06:00

131 lines
3.7 KiB
Swift

//
// LabelViewController.swift
// VDSSample
//
// Created by Matt Bruce on 8/16/22.
//
import Foundation
import UIKit
import VDS
import VDSColorTokens
class LabelViewController: BaseViewController {
lazy var textSizePickerSelectorView = {
TextSizePickerSelectorView(title: "",
picker: self.picker)
}()
lazy var fontCategoryPickerSelectorView = {
FontCategoryPickerSelectorView(title: "",
picker: self.picker)
}()
var boldSwitch = Toggle()
var disabledSwitch = Toggle()
var textField = TextField()
private var isBold: Bool = false
var label = Label()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: label)
setupForm()
setupPicker()
setupModel()
}
override func allTextFields() -> [TextField]? { [textField] }
func setupForm(){
addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
addFormRow(label: "Bold", view: .makeWrapper(for: boldSwitch))
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Font Category", view: fontCategoryPickerSelectorView)
addFormRow(label: "Text Size", view: textSizePickerSelectorView)
addFormRow(label: "Text", view: textField)
disabledSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.label.disabled = sender.isOn
}.store(in: &subscribers)
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?.label.text = text
}.store(in: &subscribers)
}
func setupModel() {
label.text = "Label Component"
label.textStyle = .featureSmall
//setup UI
surfacePickerSelectorView.text = label.surface.rawValue
disabledSwitch.isOn = label.disabled
boldSwitch.isOn = isBold
textField.text = label.text
//set the font
fontCategory = .feature
fontCategoryPickerSelectorView.text = "Feature"
textSize = .small
textSizePickerSelectorView.text = "Small"
}
//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) {
label.textStyle = style
}
}
func setupPicker(){
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.label.surface = item
self?.contentTopView.backgroundColor = item.color
}
textSizePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.textSize = item
}
fontCategoryPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.fontCategory = item
}
}
}